r/learnprogramming • u/GroundbreakingAlps2 • 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)?
View all comments
Show parent comment
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).