迭代器的性能优势? [英] Performance Advantages to Iterators?

查看:135
本文介绍了迭代器的性能优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用迭代器可以提供什么(如果有的话)性能优势。它似乎是解决许多问题的正确方法,但它是否会创建更快/更具记忆意识的代码?我在Python中专门思考,但不限制答案。

What (if any) performance advantages are offered by using iterators. It seems like the 'Right Way' to solve many problems, but does it create faster/more memory-conscious code? I'm thinking specifically in Python, but don't restrict answers to just that.

推荐答案

python邮件列表上有一个非常好的邮件:迭代器与列表。它有点过时(从2003年开始),但据我所知,它仍然有效。

There's actually a very good mail on the python mailing list about this: Iterators vs Lists. It's a bit dated (from 2003), but as far as I know, it's still valid.

以下是摘要:


对于小型数据集,迭代器和基于列表的方法具有类似的
性能。
对于较大的数据集,迭代器可以节省时间和空间。

For small datasets, iterator and list based approaches have similar performance. For larger datasets, iterators save both time and space.

我将从中得出的是:迭代器是如果可能的话,首选将数据加载到列表中。但除非你有一个大的数据集,否则不要扭曲你的代码来制作一个适合列表的东西来处理迭代器。

What I would draw from it is this: iterators are to be preferred over loading data into a list if possible. But unless you have a big dataset, don't contort your code to make something that should fit in a list to work with an iterator.

这篇关于迭代器的性能优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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