遍历生成器和列表之间的速度差异 [英] Speed difference between iterating over generators and lists

查看:72
本文介绍了遍历生成器和列表之间的速度差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的简单示例中,有两个函数对随机数列表进行排序.第一种方法传递 sorted 生成器表达式,第二种方法创建首先列出:

In the following trivial examples there are two functions that sort a list of random numbers. The first method passes sorted a generator expression, the second method creates a list first:

import random
l = [int(1000*random.random()) for i in xrange(10*6)]

def sort_with_generator():
    return sorted(a for a in l)

def sort_with_list():
    return sorted([a for a in l])

使用 line profiler 进行基准测试,表明第二个选项(sort_with_list)的运行速度大约是生成器表达式.

Benchmarking with line profiler indicates that the second option (sort_with_list) is about twice as fast as the generator expression.

谁能解释发生了什么,为什么第一种方法比第二种方法慢得多?

Can anyone explain what's happening, and why the first method is so much slower than the second?

推荐答案

第一个示例是在列表上迭代的生成器表达式.您的第二个示例是一个遍历列表的列表表达式.确实,第二个示例稍快一些.

Your first example is a generator expression that iterates over a list. Your second example is a list expression that iterates over a list. Indeed, the second example is slightly faster.

>>> import timeit
>>> timeit("sorted(a for a in l)", setup="import random;l = [int(1000*random.random()) for i in xrange(10*6)]")
5.963912010192871
>>> timeit("sorted([a for a in l])", setup="import random;l = [int(1000*random.random()) for i in xrange(10*6)]")
5.021576881408691

毫无疑问,这样做的原因是一次性创建列表,而在生成器上进行迭代则需要调用函数.

生成器不会加快这样的小列表的速度(列表中有60个元素,这非常小).主要是在创建长列表时节省内存.

Generators are not to speed up small lists like this (you have 60 elements in the list, that's very small). It's to save memory when creating long lists, primarily.

这篇关于遍历生成器和列表之间的速度差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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