![](/img/trans.png)
[英]Why is dict definition faster in Python 2.7 than in Python 3.x?
[英]Why is Python 2.7 faster than 3.2?
我编写了以下程序作为Project Euler问题12的解决方案,但在Python 2.7中花费了6.62秒,而在Python 3.2中花费了10.21秒。 当然应该反过来!
import time
def mainrun():
start = time.time()
divnum = 0
i = 0
trinum = 0
while divnum < 501:
i += 1
trinum += i
divnum = 0
#2nd arg outside - no diff to speed
for j in range(1, int(trinum**.5)+1):
if trinum % j == 0:
divnum += 1
if trinum / j != j:
divnum += 1
print(trinum, '\nDivisors:', divnum)
print('Solved in', round((time.time()-start),2), 'seconds.')
mainrun()
有谁知道为什么更高版本的Python较慢?
Python3 int类型以前是Python2 long类型。 长整数比整数慢。 Python是为简单而不是速度而优化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.