在Linux上需要1.09171080828秒.在Windows上需要2.14042000294秒.基准代码:import timedef mk_array(num):return [x for x in xrange(1,num)]def run():arr = mk_array(10000000)x = 0start = time.time()x = red...

在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.
取自wiki/64-bit_computing
本文标题为:Windows(64位)上的Python 2.7.8(64位)是Ubuntu(64位)上2.7.8(64位)的一半


基础教程推荐
- python 内置函数-range()+zip()+sorted()+map()+reduce()+filter() 2023-08-09
- CentOS7 安装 Python3.6 2023-09-04
- centos7 python2和python3共存 2023-09-03
- Python实现发送警告通知到企业微信方法详解 2023-08-11
- Python Django获取URL中的数据详解 2023-08-11
- python多进程并发 2023-09-03
- Python 多进程multiprocessing 2023-09-04
- python数据可视化Seaborn绘制山脊图 2023-08-04
- Python实现图片与视频互转代码实战(亲测有效) 2023-08-11
- Python OpenCV特征检测之特征匹配方式详解 2023-08-08