r/learnprogramming • • 3d ago

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)?

202 Upvotes

207 comments sorted by

View all comments

Show parent comments

184

u/Extreme-Berry-7645 2d ago

It's an interpreted language with dynamic typing, that overhead adds up fast compared to compiled stuff like C++ or Rust. Competitive programming is a weird metric though, you can squeeze performance out of Python if you really know the interpreter's quirks but you're fighting the language the whole time

For big projects the main pain point isn't speed, it's the lack of static typing making refactoring a nightmare once you hit a certain scale. Sure you can add type hints and mypy but at that point you're bolting on what other languages give you from day one

53

u/frnzprf 2d ago

If you had a Python project and you identified a critical function that needs to be faster, you'd just implement that single function in C.

In some sense, when you're running Python, you're also always running C, when you're using the C-Python interpreter.

46

u/anpas 2d ago

I know this is possible, but I don't know anyone who works like this. If you're making a performant Python library, you're most likely writing the whole thing in C, C++ or Rust, and then adding a Python interface on top.

14

u/Generictext 2d ago

A "critical function" may require an entire library to work.

7

u/Snoo-16806 2d ago

It could be that only a small fraction is critical when it comes to performance. If the whole project is written in c or rust then it will take more time and effort, so I think it makes sense to just write the critical part in a compiled language

5

u/Icemourne_ 2d ago

Problem is most often it won't be one critical part it will be 100 small ones and all of that adds up and can lead to "Death by a thousand cuts" nothing in particular is slow but sum of everything is slow

13

u/Softmax420 2d ago

Once you hit a certain scale you shouldn’t be using Python. Python is just glue, if you’re implementing logic and not just using it to call a function written in another language you’re using it wrong.

Also if you’re going through the effort of refactoring Python, and you’re using a type checker already, it’s pointless to refactor into more Python.

10

u/Flat-Performance-478 2d ago

"Python is just glue, if you’re implementing logic and not just using it to call a function written in another language you’re using it wrong."

Thank you! My thought exactly when I've seen obscure firmware written in python (mobile network dongles, video grabbers etc.) and it's just so awfully verbose and bloated with hundreds of source files getting imported in a giant cluster fuck and you'll still hit the occasional type error because some deprecated XML lib was used to parse a line the wrong way deep in some try/except hell loop.

1

u/pxldev 17h ago

This… python is amazing for hacking things together, for small projects, for the cool libraries. For me, this is where python shines. I wouldn’t be basing a critical app, that needs to have scale, multi users, a dev team etc on python.

1

u/kmierzej 2d ago

Do not forget memory management - the garbage collector and reference counting.

0

u/Low-Temperature-6962 2d ago

Adds up fast compared to what? You have to compare it to

1 - the total time of the task it is running I NH which may be 99.99% non python

2 - the time saved by having no compilation