snoack / python-goto

A function decorator, that rewrites the bytecode, to enable goto in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NOPs slow down my algorithm

ztane opened this issue · comments

They take unnecessary clock cycles; you should rewrite the bytecode so that it does not have NOPs

Fixed by 2b0f5e5. Note that there are still NOP instructions in the generated code, but they are never executed, as they are preceded by JUMP_FORWARD or JUMP_ABSOLUTE instructions.

I did run the example from the README, with %timeit range(0, 1000) in ipython:

 10000 loops, best of 3: 72.9 µs per loop @ CPython 2.7.10 with NOP
 10000 loops, best of 3: 77.2 µs per loop @ CPython 2.7.10 with JUMP_FORWARD
 10000 loops, best of 3:  106 µs per loop @ CPython 3.5.0  with NOP
 10000 loops, best of 3:  106 µs per loop @ CPython 3.5.0  with JUMP_FORWARD
100000 loops, best of 3:  8.6 µs per loop @ PyPy 2.4.0     with NOP
100000 loops, best of 3:  8.7 µs per loop @ PyPy 2.4.0     with JUMP_FORWARD

So it turned out that one JUMP_FORWARD instruction isn't any faster than 7 NOPs. In Python 2.7, JUMP_FORWARD is even slower. So I reverted the change: d19d244.

Alternatively, one could strip the code completely instead replacing it. However, the you have to adjust jump targets, which isn't probably worth it.