本文通过比较三种打包方式,来对比哪种打包方式打包的程序性能最好。
测试方式:
本文使用pystone.py来进行测试。
Pystone.py是一个经典的 Python 性能测试脚本,最早由 Tim Peters 编写,用作衡量 Python 解释器性能的一个基准测试工具。Pystone 的名称来源于早期的 Ada 语言中的 PicoStone benchmark,它试图通过一系列的抽象操作(如对象创建、方法调用等)来模拟典型 Python 程序的运算特征。
它包含一系列操作,这些操作被设计成能够尽可能涵盖多种类型的操作和数据结构,以便全面反映 Python 解释器在处理不同类型任务时的速度。Pystone 的执行结果通常以每秒执行的 Pystone 值(Pystones/second)为单位,不同版本的 Python 或不同硬件平台上,Pystone 值越高则表明解释器性能越好。
tips:
随着时间推移和技术进步,pystone.py已经不再是最适合评估现代 Python 解释器性能的基准之一,因为它的设计相对简单,没有充分考虑到现代 CPU 架构和 Python 优化技术带来的影响。本文用它也仅仅是因为做个粗浅的测评。
以其输出的machine benchmarks作为性能衡量标准。该值越大,说明性能越好。
测试平台:
win11,python 3.8.18,pyinstaller 6.5.0 ,nuitka 2.1.5。
环境:使用conda新建python 3.8.18虚拟环境。
数据处理:以运行100次pystone.py为计算考量,统计machine benchmarks最大值,最小值,平均值。
代码架构:
包含main.py,pystone.py两个py文件。
main.py
import time
from pystone import ceshi
ceshi()
pystone.py
#!/usr/bin/env python3
"""
"PYSTONE" Benchmark Program
Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013.
Translated from ADA to C by Rick Richardson.
Every method to preserve ADA-likeness has been used,
at the expense of C-ness.
Translated from C to Python by Guido van Rossum.
Version History:
Version 1.1 corrects two bugs in version 1.0:
First, it leaked memory: in Proc1(), NextRecord ends
up having a pointer to itself. I have corrected this
by zapping NextRecord.PtrComp at the end of Proc1().
Second, Proc3() used the operator != to compare a
record to None. This is rather inefficient and not
true to the intention of the original benchmark (where
a pointer comparison to None is intended; the !=
operator attempts to find a method __cmp__ to do value
comparison of the record). Version 1.1 runs 5-10
percent faster than version 1.0, so benchmark figures
of different versions can't be compared directly.
"""
from __future__ import print_function
from time import perf_counter as clock
LOOPS = 50000
__version__ = "1.1"
[Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
class Record(object):
def __init__(self, PtrComp=None, Discr=0, EnumComp=0,
IntComp=0, StringComp=0):
self.PtrComp = PtrComp
self.Discr = Discr
self.EnumComp = EnumComp
self.IntComp = IntComp
self.StringComp = StringComp
def copy(self):
return Record(self.PtrComp, self.Discr, self.EnumComp,
self.IntComp, self.StringComp)
TRUE = 1
FALSE = 0
def main(loops=LOOPS):
benchtime, stones = pystones(loops)
print("Pystone(%s) time for %d passes = %g" % \
(__version__, loops, benchtime))
print("This machine benchmarks at %g pystones/second" % stones)
return stones
def pystones(loops=LOOPS):
return Proc0(loops)
IntGlob = 0
BoolGlob = FALSE
Char1Glob = '\0'
Char2Glob = '\0'
Array1Glob = [0] * 51
Array2Glob = [x[:] for x in [Array1Glob] * 51]
PtrGlb = None
PtrGlbNext = None
def Proc0(loops=LOOPS):
global IntGlob
global BoolGlob
global Char1Glob
global Char2Glob
global Array1Glob
global Array2Glob
global PtrGlb
global PtrGlbNext
starttime = clock()
for i in range(loops):
pass
nulltime = clock() - starttime
PtrGlbNext = Record()
PtrGlb = Record()
PtrGlb.PtrComp = PtrGlbNext
PtrGlb.Discr = Ident1
PtrGlb.EnumComp = Ident3
PtrGlb.IntComp = 40
PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
Array2Glob[8][7] = 10
starttime = clock()
for i in range(loops):
Proc5()
Proc4()
IntLoc1 = 2
IntLoc2 = 3
String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
EnumLoc = Ident2
BoolGlob = not Func2(String1Loc, String2Loc)
while IntLoc1 < IntLoc2:
IntLoc3 = 5 * IntLoc1 - IntLoc2
IntLoc3 = Proc7(IntLoc1, IntLoc2)
IntLoc1 = IntLoc1 + 1
Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
PtrGlb = Proc1(PtrGlb)
CharIndex = 'A'
while CharIndex <= Char2Glob:
if EnumLoc == Func1(CharIndex, 'C'):
EnumLoc = Proc6(Ident1)
CharIndex = chr(ord(CharIndex) + 1)
IntLoc3 = IntLoc2 * IntLoc1
IntLoc2 = IntLoc3 / IntLoc1
IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
IntLoc1 = Proc2(IntLoc1)
benchtime = clock() - starttime - nulltime
if benchtime == 0.0:
loopsPerBenchtime = 0.0
else:
loopsPerBenchtime = (loops / benchtime)
return benchtime, loopsPerBenchtime
def Proc1(PtrParIn):
PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
PtrParIn.IntComp = 5
NextRecord.IntComp = PtrParIn.IntComp
NextRecord.PtrComp = PtrParIn.PtrComp
NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
if NextRecord.Discr == Ident1:
NextRecord.IntComp = 6
NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
NextRecord.PtrComp = PtrGlb.PtrComp
NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
else:
PtrParIn = NextRecord.copy()
NextRecord.PtrComp = None
return PtrParIn
def Proc2(IntParIO):
IntLoc = IntParIO + 10
while 1:
if Char1Glob == 'A':
IntLoc = IntLoc - 1
IntParIO = IntLoc - IntGlob
EnumLoc = Ident1
if EnumLoc == Ident1:
break
return IntParIO
def Proc3(PtrParOut):
global IntGlob
if PtrGlb is not None:
PtrParOut = PtrGlb.PtrComp
else:
IntGlob = 100
PtrGlb.IntComp = Proc7(10, IntGlob)
return PtrParOut
def Proc4():
global Char2Glob
BoolLoc = Char1Glob == 'A'
BoolLoc = BoolLoc or BoolGlob
Char2Glob = 'B'
def Proc5():
global Char1Glob
global BoolGlob
Char1Glob = 'A'
BoolGlob = FALSE
def Proc6(EnumParIn):
EnumParOut = EnumParIn
if not Func3(EnumParIn):
EnumParOut = Ident4
if EnumParIn == Ident1:
EnumParOut = Ident1
elif EnumParIn == Ident2:
if IntGlob > 100:
EnumParOut = Ident1
else:
EnumParOut = Ident4
elif EnumParIn == Ident3:
EnumParOut = Ident2
elif EnumParIn == Ident4:
pass
elif EnumParIn == Ident5:
EnumParOut = Ident3
return EnumParOut
def Proc7(IntParI1, IntParI2):
IntLoc = IntParI1 + 2
IntParOut = IntParI2 + IntLoc
return IntParOut
def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
global IntGlob
IntLoc = IntParI1 + 5
Array1Par[IntLoc] = IntParI2
Array1Par[IntLoc + 1] = Array1Par[IntLoc]
Array1Par[IntLoc + 30] = IntLoc
for IntIndex in range(IntLoc, IntLoc + 2):
Array2Par[IntLoc][IntIndex] = IntLoc
Array2Par[IntLoc][IntLoc - 1] = Array2Par[IntLoc][IntLoc - 1] + 1
Array2Par[IntLoc + 20][IntLoc] = Array1Par[IntLoc]
IntGlob = 5
def Func1(CharPar1, CharPar2):
CharLoc1 = CharPar1
CharLoc2 = CharLoc1
if CharLoc2 != CharPar2:
return Ident1
else:
return Ident2
def Func2(StrParI1, StrParI2):
IntLoc = 1
while IntLoc <= 1:
if Func1(StrParI1[IntLoc], StrParI2[IntLoc + 1]) == Ident1:
CharLoc = 'A'
IntLoc = IntLoc + 1
if CharLoc >= 'W' and CharLoc <= 'Z':
IntLoc = 7
if CharLoc == 'X':
return TRUE
else:
if StrParI1 > StrParI2:
IntLoc = IntLoc + 7
return TRUE
else:
return FALSE
def Func3(EnumParIn):
EnumLoc = EnumParIn
if EnumLoc == Ident3: return TRUE
return FALSE
def ceshi():
import sys
def error(msg):
print(msg, end=' ', file=sys.stderr)
print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr)
sys.exit(100)
nargs = len(sys.argv) - 1
if nargs > 1:
error("%d arguments are too many;" % nargs)
elif nargs == 1:
try:
loops = int(sys.argv[1])
except ValueError:
error("Invalid argument %r;" % sys.argv[1])
else:
loops = LOOPS
result = []
for i in range(100):
result.append(main(loops))
print("平均结果", sum(result) / len(result))
print("最大值", max(result))
print("最小值", min(result))
with open("result.txt", "w", encoding="utf-8") as file:
file.write("平均结果" + str(sum(result) / len(result)) + "\n")
file.write("最大值" + str(max(result)) + "\n")
file.write("最小值" + str(min(result)) + "\n")
测试打包类型:
作为对比,以下增加pycharm作为参照组。
第一组
1,pyinstaller+py:使用pyinstaller将main.py打包为可执行文件(多文件形式);
2,nuitka-nofollow:使用nuitka --nofollow-imports将main.py打包为可执行文件(依赖本机环境);
3,nuitka-standalone:使用nuitka --standalone 将main.py打包为可执行文件(不依赖本机环境);
4,嵌入式+py:使用Python 3.8.10 embeddable解释器将main.py打包为独立程序;
5,pycharm+py:使用pycharm运行main.py;
第二组
使用nuitka --module pystone.py 命令将pystone.py编译为pystone.cp38-win_amd64.pyd文件,然后再打包。
1,pyinstaller+pyd:同上。
2,nuitka-nofollow:同上。
3,nuitka-standalone:同上。
4,嵌入式+pyd:同上。
5,pycharm+pyd:同上。
测试结果如下:
benchmarks | |||
测试类型 | 最小值 | 最大值 | 平均值 |
pyinstaller+py | 149018.8 | 228191 | 208592.6 |
nuitka-nofollow | 196698.6 | 227482.9 | 215692.8 |
nuitka-standalone | 315474.1 | 464840.4 | 451053.6 |
嵌入式+py | 241302.6 | 383055.2 | 354852.4 |
pycharm+py | 198568.5 | 225326.1 | 215501.4 |
将pystone使用nuitka转为pyd文件 | |||
pyinstaller+pyd | 308124.1 | 461324.4 | 434051 |
nuitka-nofollow | 264691.2 | 468959.6 | 446176.9 |
nuitka-standalone | 238961.6 | 461419.8 | 443570.9 |
嵌入式+pyd | 372779.6 | 598229.2 | 558092.3 |
pycharm+pyd | 434866.9 | 469542.6 | 455194.4 |
数据处理
将pycharm+py的平均值测试数据设定为100%,归一化处理其他数据结果如下:
benchmarks | |||
测试类型 | 最小值 | 最大值 | 平均值 |
pyinstaller+py | 69.15% | 105.89% | 96.79% |
nuitka-nofollow | 91.27% | 105.56% | 100.09% |
nuitka-standalone | 146.39% | 215.70% | 209.30% |
嵌入式+py | 111.97% | 177.75% | 164.66% |
pycharm+py | 92.14% | 104.56% | 100.00% |
将pystone使用nuitka转为pyd文件 | |||
pyinstaller+pyd | 142.98% | 214.07% | 201.41% |
nuitka-nofollow | 122.83% | 217.61% | 207.04% |
nuitka-standalone | 110.89% | 214.11% | 205.83% |
嵌入式+pyd | 172.98% | 277.60% | 258.97% |
pycharm+pyd | 201.79% | 217.88% | 211.23% |
将第一组测试结果绘图如下:
将第二组的测试结果绘图如下:
结论
可以直观的看出:
1,将py文件转为pyd文件可以显著提升性能。
2,py文件不单独转为pyd的话,使用nuitka打包程序,性能提升明显。
3,将py文件转为pyd之后,使用嵌入式+pyd的打包模式,性能遥遥领先于其他的打包办法。
那么问题来了,怎么实现嵌入式打包呢?请看下面的文章: