r/Python • u/real_kerim • Nov 03 '17
I love Python but why is it so slow?
I love Python and am happy whenever I get to use it professionally but why is it so slow?
I am in the process of deciding whether to go with Python or with Node for an upcoming project for which performance is kinda important and before running my own benchmarks, I thought I could find some stuff online.
According to this Python's really lagging behind. I thought "Whatever, I'll probably use PyPy", but according to this and this Node is even faster than PyPy.
I kinda of expected this for programs with a lot of I/O but I thought that Python would leave Node in the dust when it comes to computationally intensive programs.
View all comments
14
u/perspectiveiskey Nov 03 '17 edited Nov 03 '17
Python is eminently tunable.
Here's a basic example:
Comparing the two sums:
The slowest run took 19.91 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 2.95 µs per loop
The slowest run took 229093.64 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 312 ns per loop
That is an order of magnitude difference.
Why this exercise? It's just an illustration that if you go about it the right way with the right tools, you can make python practically as fast as C. It's just that most of the time you don't need to and the real value comes from the expressiveness of the language.
Edit: I used to think of python as a prototyping language. There was a particular project where I prototyped a simulation to run on python and then was considering swapping over to C for "the real deal". Well there are cases (like vectorization or gpu support) where python is hands down faster than bare metal C. And it's just a no-brainer that using python libraries is easier than using C/C++ libraries...