r/Python • u/jakevdp • Jun 15 '13
Numba: 1400x Speedup of Python code in a single line
http://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/20
u/Enginoob Jun 15 '13
Great article OP (you've been killing it with your blog posts recently).
I think what's perhaps most impressive about this is how easy they've made it to use (once you get it compiled, that is). 1400x speed up by adding a single decorator? For scientists and engineers that want to focus on implementing their research, and not necessarily execution efficiency, this could be huge.
4
u/Ph0X Jun 15 '13
I'm curious as to how close to perfection this is. Since this is a fairly straight forward computation, it shouldn't be too hard to write optimized C or even assembly code to do it. I'm curious what the best we can achieve is, and how many % off they are from that here.
1
u/catcradle5 Jun 16 '13
I'm sure you could make it a bit faster by implementing the entire thing in optimized C and importing it from Python with the most minimal usage of Python objects as possible, but that sort of defeats the purpose. (Of course, he did do an example in Fortran so perhaps it'd be worth writing it in C just to see how it compares.)
2
u/xcbsmith Jun 16 '13
Did you guys read the entire article? The Scipy function is a Python wrapper of C code.
1
u/catcradle5 Jun 16 '13
It is listed as a "Python wrapper of C code." Much of CPython's standard lib is a Python wrapper of C code; that description isn't really too specific.
The core implementation would need to be posted to really compare.
1
u/jakevdp Jun 17 '13
The Scipy implementation can be seen here: https://github.com/scipy/scipy/blob/master/scipy/spatial/src/distance.c#L652
1
u/Ph0X Jun 16 '13
Yes, but I was asking how much faster. If we give up Python completely and write write pure assembly, what's the best we can hope to ever run this computation at. I want to see how close to the "best" Numba is right now.
And by optimized, I really meant "provably optimal".
1
u/alcalde Jun 17 '13
And by optimized, I really meant "provably optimal".
We're Python users. We're supposed to laugh at people who bit twiddle for nanoseconds.
7
u/Wonnk13 Jun 15 '13
I don't undertand how Fortran isn't the fastest
8
u/Migglepuff Jun 15 '13
I think a number of things could be at play here:
The f2py wrapper can have a substantial overhead (in some cases ~.5 s).
The fortran code might not be built with optimization. In addition, the Intel fortran compiler can produce much faster code than gfortran, though likely not in this case.
The loop in this code does not take memory alignment into account like scipy's backend does. (More about this and other effects can be found here. This is a reference on matrix-matrix multiplication, but many of the same principles apply.)
-1
u/roger_ Jun 17 '13
Why would Fortran be faster that straight C in the example he used (basic loops and calculations)?
4
Jun 16 '13
How does PyPy 2.0.2 compare with this in using the Pure Python only version?
1
u/lloyd-in-awe Jun 16 '13
Ctrl+F Pypy...
PyPy is extremely interesting. However, it's currently all but useless for scientific applications, because it does not support NumPy[.]
0
Jun 16 '13
[deleted]
0
u/roger_ Jun 17 '13
Have you guys been keeping up with the new WebAgg Matplotlib backend?
Would that be easier to support in PyPy?
2
Jun 16 '13
Very interesting, thanks op. I tried to install it for my python environment but couldn't, something fails when installing the pyllvm, then I tried it with anaconda and worked well enough. However, I tried to build some functions with really basic stuff like concatenating strings and it didn't work, others did work.
1
0
Jun 15 '13
[deleted]
3
u/flying-sheep Jun 15 '13
In the footer: “Powered by Pelican”
It’s a static site generator, just like Github’s Jekyll, but you have to run it manually instead of Github doing it for you ;)
2
2
18
u/[deleted] Jun 16 '13
I was at the Numba PyCon talk.
Then I went home and tried using it and it threw an exception for every non-trivial function I decorated.
Maybe I just write tricky code.