why does dead code costs time?
python.ml.bruno.dupuis at lisael.org
Wed Dec 5 16:46:39 CET 2012
I'm interested in compilers optimizations, so I study python compilation process
I ran that script:
number = 10000
print(timeit.timeit('f(1)',setup="from __main__ import f", number=number))
print(timeit.timeit('g(1)',setup="from __main__ import g", number=number))
It gives this output:
17 0 LOAD_CONST 0 (None)
20 0 LOAD_GLOBAL 0 (None)
21 4 LOAD_GLOBAL 1 (print)
7 LOAD_FAST 0 (x)
10 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
I do not understand why the dead code `print(x)` takes time (~20% in
that case). As we see in the opcode, a call to g(1) returns immediately, so
there should be no delay at all. Where am i wrong?
mmhh... it comes to me now that the gap must be in function loading time...
I'll check ceval.c
However, isn't there a room for a slight optim here? (in this case, the
dead code is obvious, but it may be hidden by complex loops and
More information about the Python-list