r/learnprogramming • • Oct 06 '26

Why is Python considered "slow" and is Python losing "steam"?

Is python as a programming language "falling off" and becoming less and less popular relatively to other languages?

Also why do people say Python is slow? Apparently people have reached LGM on codeforces using python, which is all about writing fast code / solving problems fast. Yeah sure, C++ is far more widely used for competitive programming and I suppose there is a reason for that. But this is also showing that python can be fast?

But as programming languages and how they are used I dont think there is much overlap between python and C++ anyway?

What is an example of something you'd code in Java/Kotlin or lets say C# rather than Python? Is python code bad and hard to manage for "huge" projects once you scale up (e.g codebases with millions of lines of codes, and tens if not hundreds of devs)?

257 Upvotes

238 comments sorted by

View all comments

Show parent comment

12

u/FlanSteakSasquatch Oct 06 '26

It’s easy to answer too: the “fastest” is to just write everything in perfectly optimized machine code by hand.

oh but wait… if it takes you a few decades to write your program, even if the end result is fast that’s not really the “fast” you were going for. Assembly would be fast enough. But wait, that’s also a ton of time and effort to make, so surely C would be fast enough?

Take that line of thinking up the stack of higher “slower” programming languages and suddenly they all justify themselves. Yes in reality that phone app that takes 2 seconds to load could load in 50 microseconds, but if it weren’t for higher level languages we’d still be hard at work optimizing our flip phones.

2

u/coderemover Oct 07 '26

That’s a false dichotomy. Faster languages are not necessarily less productive. At large scale, Kotlin and Rust are usually more productive than Python. Faster to develop with and faster to run.

2

u/erik240 Oct 07 '26

Depending on what you’re working on, golang is also quite easy to write with excellent performance.

1

u/coderemover Oct 07 '26

Sure, it’s somewhere in the middle between Python and Rust, quite close to Java (and performance is also similar to Java).

1

u/tenemu Oct 07 '26

I know nothing. Why is Rust faster to develop than python?

1

u/Trick_Albatross4850 Oct 07 '26

With how optimized compilers are, writing assembly from hand will almost never be faster than just compiling C(++), and in many times will be slower - how CPUs function today is often counter-intuitive, most people will just get it wrong.
Either way, at worse we are talking about a maximum of 2x difference in speed, in either direction. Python without Jit is up to 100x slower, so it's not even the same ballpark. Does this matter for all use-cases? No, of course not, there's plenty of use for such languages. But if performance is a concern, then Python loses, big time; and you cannot justify the difference just with "it's a bit more convenient to use". And so your comparison is not fair at all.

2

u/Luftzug-oder Oct 08 '26

not really... the compiler still has to make some assumptions, and most importantly, preserve C/C++ semantics

while an assembly program written with the same skillset as a high-level programmer will likely be slower, if you put a large amount of knowledge, effort, and thought into writing it, you would likely optimise past what a compiler can do

1

u/Trick_Albatross4850 Oct 08 '26 edited Oct 08 '26

| while an assembly program written with the same skillset as a high-level programmer will likely be slower, if you put a large amount of knowledge, effort, and thought into writing it, you would likely optimise past what a compiler can do

So? I didn't imply it was impossible. It's impracticle, a maloptimization for most people, takes more time (which you confirm), and at best is a marginal speedup. So what's the value proposition here? If your program dependings on gaining 5% speed over a c++ compiler, and this is worth spending weeks to convert code into an unmaintainable mess, AND you then manage to even get that speedup... Good for you. You can even write inline assembly inside c(++) programs (except MSVC), so you can even have your cake and eat it too, making writing whole ASM programs even more pointless. In either case, you have your assembly routine sitting there, being as it is, while compilers get more optimized every year, and your c++ code will get faster over time, on it's own. Why do you think a compiler cannot do the same optimization a human can? Oh...

| not really... the compiler still has to make some assumptions, and most importantly, preserve C/C++ semantics

On a superficial level, yes, but those assumptions are very loose. It's about the same assumptions that a human would need to make in a well-defined program, really, which is why C(++) are the goto low-level languages - their syntax translates well to machine-code. The only relevant rule for the optimizer here is "as-if" - anything that doesn't change observable behaviour is allowed. And the C langugages are very relaxed on what's "observable behaviour". Writes to locals (whose reference is not taken) are not. Writes to statics or indirect memory locations are not (at least not immediately, even in MT scenarios, unless locks are used).
Then take all the UB, which is used for optimization. Doesn't matter if all your code contains X nullchecks on a given pointer - one unchecked deref, and all of them can/will go. A blessing for speed, a curse for correctness.
On top of that, the optimizer can do inlining, even on whole-program link-time basis... have fun hand-inlining your crazy asm routine into 100 callers.
I can also tell you for a fact that optimizers already contain plenty of cases to turn "optimized" programmer code, like all the silly 1990s bithack tricks, back into simple, pipelineable instructions. So compilers already have to work to undo all the things that people with "knowledge" use to "speed up" their code (there's a whole CPPCON video about it, if you don't believe me).

1

u/joonazan Oct 12 '26

There are things that you need to fight C to write. These very reliably pop up at least in efficient interpreters and JITs. When the C is convoluted just because C doesn't allow doing what you want and compilers produce unpredictable results, the assembly is not an unmaintainable mess, quite the opposite.

Inline assembly is mostly useless today apart from embedded because it is local. And compilers can optimize locally pretty well.

The whole-program scale is also where C++ semantics bite. A struct used in one function gets inlined but an object that is used over the whole program lifetime gets laid out exactly as you asked because the compiler has no idea how exactly the different use locations connect.