【发布时间】:2014-09-17 13:14:32
【问题描述】:
在我的 Windows 7 机器上,我使用了两个 CPython 实现:
1)WinPython distribution,使用MSC v.1500 64bit编译
2)MinGW-builds,使用MinGW/GCC 4.9.1 64bit编译
我已尝试使用 MinGW 构建的版本为 Python 编译一些 C 扩展,这些扩展需要使用与 Python 本身相同的编译器构建才能正常运行。
现在考虑以下测试脚本,它会生成一个随机字典并反复腌制和取消腌制它。
import pickle, cPickle, random
from time import clock
def timeit(mdl, d, num=100, bestof=10):
times = []
for _ in range(bestof):
start = clock()
for _ in range(num):
mdl.loads(mdl.dumps(d))
times.append(clock() - start)
return min(times)
def gen_dict(entries=100, keylength=5):
formatstr = "{:0%dx}" % keylength
d = {}
for _ in range(entries):
rn = random.randrange(16**keylength) # 'keylength'-digit hex number
# format into string of length 5 as key, decimal value as value
d[formatstr.format(rn)] = rn
return d
def main(entries=100, keylength=5, num=100, bestof=10):
print "Dict size: %d entries, keylength: %d" % (entries, keylength)
print ("Test is %d times pack/unpack. "
"Take best time out of %d runs\n" % (num, bestof))
d = gen_dict(entries, keylength)
for mdl in [pickle, cPickle]:
print "%s: %f s" % (mdl.__name__, timeit(mdl, d, num, bestof))
if __name__ == "__main__":
main()
MSC CPython 给了我
Dict size: 100 entries, keylength: 5
Test is 100 times pack/unpack. Take best time out of 10 runs
pickle: 0.107798 s
cPickle: 0.011802 s
MinGW/GCC CPython 给了我
Dict size: 100 entries, keylength: 5
Test is 100 times pack/unpack. Take best time out of 10 runs
pickle: 0.103065 s
cPickle: 0.075507 s
因此 cPickle 模块(Python 的标准库 C 扩展)在 MinGW 上比在 MSC 上慢 6.4 倍。
我没有进一步调查(即测试更多的 C 扩展),但我很惊讶。
这是意料之中的事吗?
其他 C 扩展在 Python/MinGW 工具链上的运行速度一般会变慢吗?
【问题讨论】:
标签: python c windows gcc mingw-w64