Python Windows(64 位)上的 2.7.8(64 位)的速度是 Ubuntu(64 位)上的 2.7.8(64 位)的一半

Python 2.7.8 (64bit) on Windows (64bit) is half as fast as 2.7.8 (64bit) on Ubuntu (64bit)

在 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 内存。

两者都是 运行ning Python 2.7.8 64 位。

为什么 windows 快了一半?

编辑:我删掉了两个零,linux 在 0.010593495369 秒内完成,windows 在 0.171899962425 秒内完成。谢谢大家,好奇心得到满足

这是因为 long in windows, in windows a long is 32 bits in unix it is 64 bits so you are hitting Arbitrary-precision_arithmetic 问题的大小越早分配成本越高。

相关问题

如果你单独对 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.

取自wiki/64-bit_computing