在Linux上需要1.09171080828秒.
在Windows上需要2.14042000294秒.
基准代码:
import time
def mk_array(num):
return [x for x in xrange(1,num)]
def run():
arr = mk_array(10000000)
x = 0
start = time.time()
x = reduce(lambda x,y: x + y, arr)
done = time.time()
elapsed = done - start
return elapsed
if __name__ == '__main__':
times = [run() for x in xrange(0,100)]
avg = sum(times)/len(times)
print (avg)
我知道GIL会创建或多或少的单线程脚本.
Windows box是我的Hyper-V主机,但是应该足够强大以完全运行单个线程的脚本. 12核2.93Ghz Intel X5670s,72GB ram等
Ubuntu VM具有4核和8GB内存.
两者都运行Python 2.7.8 64位.
为什么Windows速度快一半?
编辑:我已经在0.010593495369秒内放了两个零和linux完成,在0.171899962425秒内放了Windows.谢谢大家,好奇心得到满足.
解决方法:
这是因为在Windows中long的大小,在Windows中,unix中的long是32位,而在Windows中是64位,因此您会更快地遇到Arbitrary-precision_arithmetic问题,这是分配成本更高的问题.
相关问题Why are python’s for loops so non-linear for large inputs
如果仅对xrange进行基准测试,您将看到很大的不同.
Windows使用LLP64的背后原因似乎与32位代码兼容:
Another alternative is the LLP64 model, which maintains compatibility with 32-bit code by leaving both int and long as 32-bit. “LL” refers to the “long long integer” type, which is at least 64 bits on all platforms, including 32-bit environments.