Python 3 的枚举速度是否比 Python 2 慢? [英] Is there a reason Python 3 enumerates slower than Python 2?

查看:37
本文介绍了Python 3 的枚举速度是否比 Python 2 慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3 在最小循环的枚举中似乎比 Python 2 慢了很多,而且随着 Python 3 的更新版本,这种情况似乎变得更糟.

Python 3 appears to be slower in enumerations for a minimum loop than Python 2 by a significant margin, which appears to be getting worse with newer versions of Python 3.

我的 64 位 Windows 机器(英特尔 i7-2700K - 3.5 GHz)上安装了 Python 2.7.6、Python 3.3.3 和 Python 3.4.0,每个都有 32 位和 64 位版本安装了 Python.虽然在内存访问的限制范围内,给定版本的 32 位和 64 位之间的执行速度没有显着差异,但不同版本级别之间存在非常显着的差异.我会让计时结果不言自明,如下所示:

I have Python 2.7.6, Python 3.3.3, and Python 3.4.0 installed on my 64-bit windows machine, (Intel i7-2700K - 3.5 GHz) with both 32-bit and 64-bit versions of each Python installed. While there is no significant difference in execution speed between 32-bit and 64-bit for a given version within its limitations as to memory access, there is a very significant difference between different version levels. I'll let the timing results speak for themselves as follows:

C:**Python34_64**python -mtimeit -n 5 -r 2 -s"cnt = 0" "for i in range(10000000): cnt += 1"
5 loops, best of 2: **900 msec** per loop

C:**Python33_64**python -mtimeit -n 5 -r 2 -s"cnt = 0" "for i in range(10000000): cnt += 1"
5 loops, best of 2: **820 msec** per loop

C:**Python27_64**python -mtimeit -n 5 -r 2 -s"cnt = 0" "for i in range(10000000): cnt += 1"
5 loops, best of 2: **480 msec** per loop

由于 Python 3 的范围"与 Python 2 的范围"不同,并且在功能上与 Python 2 的xrange"相同,因此我也将其计时如下:

Since the Python 3 "range" is not the same as Python 2's "range", and is functionally the same as Python 2's "xrange", I also timed that as follows:

C:**Python27_64**python -mtimeit -n 5 -r 2 -s"cnt = 0" "for i in **xrange**(10000000): cnt += 1"
5 loops, best of 2: **320 msec** per loop

很容易看出,3.3 版的速度几乎是 2.7 版的两倍,而 Python 3.4 又比这慢了大约 10%.

One can easily see that version 3.3 is almost twice as slow as version 2.7 and Python 3.4 is about 10% slower than that again.

我的问题:是否有环境选项或设置可以纠正这个问题,或者只是低效的代码或解释器为 Python 3 版本做的更多?

My question: Is there an environment option or setting that corrects this, or is it just inefficient code or the interpreter doing more for the Python 3 version?

答案似乎是 Python 3 使用了在 Python 2.x 中曾经被称为long"的无限精度"整数,它的默认int"类型没有任何使用 Python 2 固定位长度的选项"int",并且处理这些可变长度的int"需要额外的时间,如下面的答案和评论中所述.

The answer seems to be that Python 3 uses the "infinite precision" integers that used to be called "long" in Python 2.x its default "int" type without any option to use the Python 2 fixed bit-length "int" and it is processing of these variable length "int"'s that is taking the extra time as discussed in the answers and comments below.

可能是 Python 3.4 比 Python 3.3 慢一些,因为内存分配的变化以支持同步,内存分配/释放稍微慢一些,这可能是当前版本的长"处理运行速度较慢的主要原因.

It may be that Python 3.4 is somewhat slower than Python 3.3 because of changes to memory allocation to support synchronization that slightly slow memory allocation/deallocation, which is likely the main reason that the current version of "long" processing runs slower.

推荐答案

不同之处在于将 int 类型替换为 long 类型.显然,使用长整数的操作会变慢,因为 long 操作更复杂.

The difference is due to the replacement of the int type with the long type. Obviously operations with long integers are going to be slower because the long operations are more complex.

如果您通过将 cnt 设置为 0L 来强制 python2 使用 longs,那么差异就会消失:

If you force python2 to use longs by setting cnt to 0L the difference goes away:

$python2 -mtimeit -n5 -r2 -s"cnt=0L" "for i in range(10000000): cnt += 1L"
5 loops, best of 2: 1.1 sec per loop
$python3 -mtimeit -n5 -r2 -s"cnt=0" "for i in range(10000000): cnt += 1"
5 loops, best of 2: 686 msec per loop
$python2 -mtimeit -n5 -r2 -s"cnt=0L" "for i in xrange(10000000): cnt += 1L"
5 loops, best of 2: 714 msec per loop

正如您在我的机器上看到的,python3.4 比使用 range 的 python2 和使用 long 时的 xrange 都快.使用 python 的 2 xrange 的最后一个基准测试表明,这种情况下的差异很小.

As you can see on my machine python3.4 is faster than both python2 using range and using xrange when using longs. The last benchmark with python's 2 xrange shows that the difference in this case is minimal.

我没有安装 python3.3,所以我无法在 3.3 和 3.4 之间进行比较,但据我所知,这两个版本之间没有任何重大变化(关于 range),所以时间应该差不多.如果您看到显着差异,请尝试使用 dis 模块.内存分配器发生了变化(PEP 445),但我不知道是否修改了默认内存分配器以及对性能有哪些影响.

I don't have python3.3 installed, so I cannot make a comparison between 3.3 and 3.4, but as far as I know nothing significant changed between these two versions (regarding range), so the timings should be about the same. If you see a significant difference try to inspect the generated bytecode using the dis module. There was a change about memory allocators (PEP 445) but I have no idea whether the default memory allocators were modified and which consequences there were performance-wise.

这篇关于Python 3 的枚举速度是否比 Python 2 慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆