问题描述
我目前正在学习 Python 3,并认为一些速度比较可能很简洁.因此,我创建了一些就地函数和一些临时列表函数,它们只是将 1 加到一长串整数中.
I am currently learning Python 3 and thought some speed comparison could be neat. Thus, I created some in-place and some temporary list functions, which simply add 1 to a long list of integers.
然而,结果真的出乎我的意料……似乎 Python 3 在每个用例中都变慢了:不同的测试循环、while 循环、map 函数和列表推导式.
However, the results were really surprising to me...it seems that Python 3 is slower in every use case: different tested for loops, a while loop, the map function, and list comprehensions.
当然(请参阅下面的代码)它只是比较就地列表突变或构建并返回临时列表的循环......所以例如Python 3 的标准"映射,只是创建一个迭代器,仍然很快,但在某些时候,人们总是喜欢打印、写入或以某种方式可视化数据.因此,像 list(map())
这样的东西在 Python 3 中应该比较常见?!
Of course (see the code below) it just comparison of in-place list mutations or loops that build up and return a temporary list...so e.g. the "standard" map of Python 3, that just creates an iterator, is still fast, but at some point one would always like to print, write or somehow visualize the data. Hence, something like list(map())
should be relatively common in Python 3?!
我在这里遗漏了什么?
对于循环、推导式和映射,Python 3 真的比 Python 2 慢吗?如果是,为什么?
Is Python 3 really slower than Python 2 for loops, comprehensions and map? If so, why?
Python 3.4 计时:
> python3.4 func_speed_tests.py
list_comprehension.........1.2785s
for_loop...................1.9988s
for_loop_append............1.8471s
for_loop_range.............1.7585s
while_loop.................2.2539s
calc_map...................2.0763s
Python 2.7 计时:
> python2.7 func_speed_tests.py
list_comprehension.........0.9472s
for_loop...................1.2821s
for_loop_append............1.5663s
for_loop_range.............1.3101s
while_loop.................2.1914s
calc_map...................1.9101s
这是代码:
import timeit
from random import shuffle
# generate a long list of integers
mlst = list(range(0, 1000000))
# randomly sort the list
shuffle(mlst)
# number of benchmark passes
n = 10
#####################
# functions to time #
#####################
def list_comprehension(lst):
return [x + 1 for x in lst]
def for_loop(lst):
for k, v in enumerate(lst):
lst[k] = v + 1
def for_loop_append(lst):
tmp = []
for item in lst:
tmp.append(item + 1)
def for_loop_range(lst):
for k in range(len(lst)):
lst[k] += 1
return lst
def while_loop(lst):
it = iter(lst)
tmp = []
while it:
try:
tmp.append(next(it)+1)
except StopIteration:
break
return tmp
def calc_map(lst):
lst = map(lambda x: x + 1, lst)
return list(lst)
############################################
# helper functions for timing and printing #
############################################
def print_time(func_name):
print("{:.<25}{:.>8.4f}s".format(func_name, timer(func_name)))
def timer(func):
return timeit.timeit(stmt="{}(mlst)".format(func),
setup="from __main__ import mlst, {}".format(func),
number=n)
#################
# run the tests #
#################
print_time("list_comprehension")
print_time("for_loop")
print_time("for_loop_append")
print_time("for_loop_range")
print_time("while_loop")
print_time("calc_map")
推荐答案
Python3 比 Python2 稍慢.查看 pystone 基准.如果速度是问题,请坚持使用 Python 2.7.0 Release,它具有更好的第三方软件包支持.
Python3 is slightly slower that Python2. Check with pystone benchmark. If speed is of concern stick with Python 2.7.0 Release which has better third party package support.
这篇关于Python 3:与 Python 2 相比,循环、列表理解和映射更慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!