Amusingly, in the first example, the compiler's static analysis concludes that the entire for loop can be omitted. I have no idea how they came up with that precise number...
Amusingly, in the first example, the compiler's static analysis concludes that the entire for loop can be omitted. I have no idea how they came up with that precise number...
def f(NUMBER):
for _ in xrange(NUMBER):
pass
runs one tenth as fast! Is it due to the xrange construct? I'm sure that there must be a saner way to make a loop in Python that doesn't bloat to ten times as many cycles as in C. . .I'd say JIT is neither about compilation nor interpretation, but about optimization.
Though that still wasn't as fast as gcc on my system (core i5 3.4ghz; using 1e8 rounds):
* cpython 2.7.4 - 1.131s
* pypy 2.6.0 - 0.090s
* gcc w/ "-O2" - 0.050s
I think the only reason they haven't is the legacy C interface; it's a shame they didn't do it for Python 3, but I guess it could have delayed conversion of c libraries even more.
You can get it back close to native speed by adding a type annotation and compiling the python file with Cython: http://docs.cython.org/src/userguide/language_basics.html#in...
def f(NUMBER):
cdef int i
for i in range(NUMBER):
passDidn't find the original talk but found this: https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Loo...
% python -mtimeit '[x for x in xrange(1000)]'
10000 loops, best of 3: 70.2 usec per loop
% python -mtimeit 'for x in xrange(1000): pass'
10000 loops, best of 3: 29 usec per loop
% python -mtimeit '[x for x in xrange(100000)]'
100 loops, best of 3: 7.03 msec per loop
% python -mtimeit 'for x in xrange(100000): pass'
100 loops, best of 3: 2.5 msec per loop