python pypy nuitka 性能对比
测试的源码
import time
def sum(n):
number = 0
for i in range(n):
number += i
return number
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
def power(n):
for i in range(10**n):
continue
return i+1
print("Test1......")
start = time.time()
print(sum(1000000000))
print(f"Ellapsed time: {time.time() - start} s")
print("Test2......")
start = time.time()
print(fibonacci(40))
print(f"Ellapsed time: {time.time() - start} s")
print("Test3......")
start = time.time()
print(power(9))
print(f"Ellapsed time: {time.time() - start} s")
版本如下:
C:\Users\Administrator>python -V
Python 3.10.11
C:\Users\Administrator>pypy -V
Python 3.10.14 (75b3de9d9035, Apr 21 2024, 13:13:38)
[PyPy 7.3.16 with MSC v.1929 64 bit (AMD64)]
C:\Users\Administrator>pip show nuitka
Name: Nuitka
Version: 2.1.6
Summary: Python compiler with full language support and CPython compatibility
Home-page: https://nuitka.net
Author: Kay Hayen
Author-email: Kay.Hayen@gmail.com
License: Apache License, Version 2.0
Location: c:\users\administrator\appdata\local\programs\python\python310\lib\site-packages
Requires: ordered-set, zstandard
Required-by:
路径设置
C:\Users\Administrator\AppData\Local\Programs\Python\Python310
C:\pypy3.10-v7.3.16-win64
对比输出结果
原生Python
C:\Users\Administrator\Desktop>python 030.py
Test1......
499999999500000000
Ellapsed time: 21.422600746154785 s
Test2......
102334155
Ellapsed time: 14.158296346664429 s
Test3......
1000000000
Ellapsed time: 7.591829538345337 s
pypy
C:\Users\Administrator\Desktop>pypy 030.py
Test1......
499999999500000000
Ellapsed time: 0.48470616340637207 s
Test2......
102334155
Ellapsed time: 1.077671766281128 s
Test3......
1000000000
Ellapsed time: 0.43866920471191406 s
Python编译后
C:\Users\Administrator\Desktop>python -m nuitka --onefile --standalone 030.py
Nuitka-Options: Used command line options: --onefile --standalone 030.py
Nuitka: Starting Python compilation with Nuitka '2.1.6' on Python '3.10' commercial grade 'not installed'.
Nuitka: Completed Python level compilation and optimization.
Nuitka: Generating source code for C backend compiler.
Nuitka: Running data composer tool for optimal constant value handling.
Nuitka: Running C compilation via Scons.
Nuitka-Scons: Backend C compiler: gcc (gcc 13.2.0).
Nuitka-Scons: Backend linking program with 6 files (no progress information available for this stage).
Nuitka-Scons: Compiled 6 C files using ccache.
Nuitka-Scons: Cached C files (using ccache) with result 'cache hit': 2
Nuitka-Scons: Cached C files (using ccache) with result 'cache miss': 4
Nuitka-Postprocessing: Creating single file from dist folder, this may take a while.
Nuitka-Onefile: Running bootstrap binary compilation via Scons.
Nuitka-Scons: Onefile C compiler: gcc (gcc 13.2.0).
Nuitka-Scons: Onefile linking program with 1 files (no progress information available for this stage).
Nuitka-Scons: Compiled 1 C files using ccache.
Nuitka-Scons: Cached C files (using ccache) with result 'cache hit': 1
Nuitka-Onefile: Using compression for onefile payload.
Nuitka-Onefile: Onefile payload compression ratio (32.45%) size 9411184 to 3053514.
Nuitka-Onefile: Keeping onefile build directory '030.onefile-build'.
Nuitka: Keeping dist folder '030.dist' for inspection, no need to use it.
Nuitka: Keeping build directory '030.build'.
Nuitka: Successfully created '030.exe'.
C:\Users\Administrator\Desktop>cd 030.dist
C:\Users\Administrator\Desktop\030.dist>030
Test1......
499999999500000000
Ellapsed time: 11.18766450881958 s
Test2......
102334155
Ellapsed time: 5.842983722686768 s
Test3......
1000000000
Ellapsed time: 5.735060930252075 s
对比
测试 | 原生Python | 用nuitka编译后 | pypy |
---|---|---|---|
累加求和 | 21.4 | 11 | 0.5 |
斐波那契数列 | 14.1 | 5.8 | 1.0 |
10的幂 | 7.6 | 5.7 | 0.4 |
结论
- 提升明显
如果用Python 3.12版本
Test1......
499999999500000000
Ellapsed time: 24.196781396865845 s
Test2......
102334155
Ellapsed time: 9.417750120162964 s
Test3......
1000000000
Ellapsed time: 11.618221759796143 s
建议
-
可见3.12版本对递归有优化,其他看不到差异。
-
因为pypy和nuitka当前主要是支持3.10,因此建议用3.10这个版本,可以进行对比和测试。
numba
如果使用numba
安装包
pip install numba
然后在源码中
from numba import jit
在三个函数前增加
@jit
C:\Users\Administrator\Desktop>python 031.py
Test1......
499999999500000000
Ellapsed time: 0.14100098609924316 s
Test2......
102334155
Ellapsed time: 0.47742629051208496 s
Test3......
1000000000
Ellapsed time: 0.13155913352966309 s
测试 | 原生Python | 用nuitka编译后 | pypy | numba |
---|---|---|---|---|
累加求和 | 21.4 | 11 | 0.5 | 0.1 |
斐波那契数列 | 14.1 | 5.8 | 1.0 | 0.5 |
10的幂 | 7.6 | 5.7 | 0.4 | 0.1 |