Python:迭代列表vs超过dict项目效率 [英] Python: iterating over list vs over dict items efficiency

查看:119
本文介绍了Python:迭代列表vs超过dict项目效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CPython中迭代超过 some_dict.items()的效率是否像在CPython中的同一项目列表一样有效?

Is iterating over some_dict.items() as efficient as iterating over a list of the same items in CPython?

推荐答案

这取决于您使用的是哪个版本的Python。在Python 2中, some_dict.items()创建一个新的列表,它占用了一些额外的时间,并占用了额外的内存。另一方面,一旦创建了列表,它就是一个列表,并且在列表创建的开销完成之后应该具有相同的性能特征。

It depends on which version of Python you're using. In Python 2, some_dict.items() creates a new list, which takes up some additional time and uses up additional memory. On the other hand, once the list is created, it's a list, and so should have identical performance characteristics after the overhead of list creation is complete.

在Python 3中, some_dict.items()创建一个视图对象而不是一个列表,我预计创建和迭代 items()将比Python 2更快,因为没有任何东西被复制。但是,我也希望在已经创建的视图上迭代将比已经创建的列表慢一点,因为字典数据有点稀疏,我相信python没有好的方法以避免迭代字典中的每个bin - 即使是空的。

In Python 3, some_dict.items() creates a view object instead of a list, and I anticipate that creating and iterating over items() would be faster than in Python 2, since nothing has to be copied. But I also anticipate that iterating over an already-created view would be a bit slower than iterating over an already-created list, because dictionary data is stored somewhat sparsely, and I believe there's no good way for python to avoid iterating over every bin in the dictionary -- even the empty ones.

在Python 2中,某些时间确认了我的直觉:

In Python 2, some timings confirm my intuitions:

>>> some_dict = dict(zip(xrange(1000), reversed(xrange(1000))))
>>> some_list = zip(xrange(1000), xrange(1000))
>>> %timeit for t in some_list: t
10000 loops, best of 3: 25.6 us per loop
>>> %timeit for t in some_dict.items(): t
10000 loops, best of 3: 57.3 us per loop

迭代项目大概是缓慢的两倍。使用 iteritems 更快一点...

Iterating over the items is roughly twice as slow. Using iteritems is a tad bit faster...

>>> %timeit for t in some_dict.iteritems(): t
10000 loops, best of 3: 41.3 us per loop

但是迭代列表本身与迭代其他列表基本相同:

But iterating over the list itself is basically the same as iterating over any other list:

>>> some_dict_list = some_dict.items()
>>> %timeit for t in some_dict_list: t
10000 loops, best of 3: 26.1 us per loop

Python 3可以比Python 2可以创建和迭代项目(与上面的57.3 us相比):

Python 3 can create and iterate over items faster than Python 2 can (compare to 57.3 us above):

>>> some_dict = dict(zip(range(1000), reversed(range(1000))))
>>> %timeit for t in some_dict.items(): t      
10000 loops, best of 3: 33.4 us per loop 

但创建视图的时间是可忽略的;实际上比列表迭代慢。

But the time to create a view is negligable; it is actually slower to iterate over than a list.

>>> some_list = list(zip(range(1000), reversed(range(1000))))
>>> some_dict_view = some_dict.items()
>>> %timeit for t in some_list: t
10000 loops, best of 3: 18.6 us per loop
>>> %timeit for t in some_dict_view: t
10000 loops, best of 3: 33.3 us per loop



<这意味着在Python 3中,如果要对字典中的项目进行多次迭代,并且性能至关重要,则可以通过将视图缓存为列表来获得30%的加速。

This means that in Python 3, if you want to iterate many times over the items in a dictionary, and performance is critical, you can get a 30% speedup by caching the view as a list.

>>> some_list = list(some_dict_view)
>>> %timeit for t in some_list: t
100000 loops, best of 3: 18.6 us per loop

这篇关于Python:迭代列表vs超过dict项目效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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