6 ms·
That's true, but most of the time you can express the "new" stuff in terms of existing fast libraries. I've had a few cases where you can't, and have become a
by ProblemFactory 10y ago
That's true, but most of the time you can express the "new" stuff in terms of existing fast libraries.
I've had a few cases where you can't, and have become a big fan of Cython for that. It lets you add C typedefs to Python code, and then compile the module at import time. Example here: http://pastebin.com/sF8KmyiU http://pastebin.com/sF8KmyiU
All of pure Python is still allowed in these modules, but the typedeffed variables become pure C variables instead of objects, and loops become pure C loops. For this particular function, I got a 1000x speedup compared to the original Python code.
In the end, this isn't Python any more - but it's close enough, and only needed for loops that run over millions of items.