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

标签 python windows benchmarking

在 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 中,long32 位,在 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.

取自wiki/64-bit_computing

关于Windows(64 位)上的 Python 2.7.8(64 位)在 Ubuntu(64 位)上的速度是 2.7.8(64 位)的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789319/

相关文章:

python - 如何使用python将NPZ文件转换为文本文件

python - 使用 python 绘制直方图时从文件中读取数据的方法?

html - 对于表格数据,什么渲染速度更快,CSS 还是 <TABLE>?

python - 如何更简洁地找到缺失值?

mysql - 最快的 SQL 全文搜索

python - savefig 未保存树状图轴

python - 设置 pandas Dataframe Boxplot() 的 y 轴刻度,3 个偏差?

c++ - 使用 C++ 连接到 PC 的设备检测

java - Gradle说我的Java安装在错误的位置,尽管它找到了正确的文件夹

python - Windows 上的 OpenAI Gym Atari