python fastloop

2019-04-11

Python FAST_LOOPS

原理

python 虚拟机里面有一个宏FAST_LOOPS,打开宏提升速度

影响和副作用

源代码注释写的很清楚了, 游戏客户端用虚拟机的时候,一般循环中不需要接收信号,所以可以打开

#if FAST_LOOPS
/ Enabling this path speeds-up all while and for-loops by bypassing
the per-loop checks for signals. By default, this should be turned-off
because it prevents detection of a control-break in tight loops like
“while 1: pass”. Compile with this option turned-on when you need
the speed-up and do not need break checking inside tight loops (ones
that contain only instructions ending with goto fast_next_opcode).
/

速度比较

  • 打开前3次
    ···
    Pystone(1.1) time for 50000 passes = 0.374852
    This machine benchmarks at 133386 pystones/second
    jhlin@tx2-dev-34c75bd1:~/pythonopcode/PythonOpcode$ python pystone.py
    Pystone(1.1) time for 50000 passes = 0.346939
    This machine benchmarks at 144118 pystones/second
    jhlin@tx2-dev-34c75bd1:~/pythonopcode/PythonOpcode$ python pystone.py
    Pystone(1.1) time for 50000 passes = 0.35255
    This machine benchmarks at 141824 pystones/second
    ···

  • 打开后3次
    ···
    jhlin@tx2-dev-34c75bd1:~/pythonopcode/PythonOpcode$ python pystone.py
    Pystone(1.1) time for 50000 passes = 0.318244
    This machine benchmarks at 157112 pystones/second
    jhlin@tx2-dev-34c75bd1:~/pythonopcode/PythonOpcode$ python pystone.py
    Pystone(1.1) time for 50000 passes = 0.320223
    This machine benchmarks at 156141 pystones/second
    jhlin@tx2-dev-34c75bd1:~/pythonopcode/PythonOpcode$ python pystone.py
    Pystone(1.1) time for 50000 passes = 0.313735
    ···

看起来还是有平均提升5%?可能和pystone本来就大量循环有关