在 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内存等
Ubuntu VM 具有 4 核和 8GB 内存。
两者都运行 Python 2.7.8 64 位。
为什么 Windows 的速度只有一半?
编辑:我去掉了两个零,Linux 在 0.010593495369 秒内完成,Windows 在 0.171899962425 秒内完成。谢谢大家,好奇心得到满足。
最佳答案
这是因为 long 的大小在 windows 中,在 windows 中,long
是 32
位,在 unix 中是 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.
关于Windows(64 位)上的 Python 2.7.8(64 位)在 Ubuntu(64 位)上的速度是 2.7.8(64 位)的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789319/