哪个更快:迭代集合和迭代列表 [英] Which one is faster: iterating over a set and iterating over a list

查看:59
本文介绍了哪个更快:迭代集合和迭代列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串列表和一组相同的字符串:

l = [str1, str2, str3, str4, ...]s = set([str1, str2, st3, str4, ...])

我需要对我拥有的短语进行字符串比较:comparephrase

我需要遍历列表或集合中的所有元素,并生成 comparephrase 和比较字符串之间的比率.我知道当我们进行成员资格测试时 set() 会更快.但是,我不是在进行成员资格测试,而是比较我拥有的短语和列表/集合中的字符串.set() 是否仍然提供更快的速度?如果是这样,为什么?在我看来,这个集合实际上是一个里面有一个列表的集合.因为我们要迭代集合中的列表,所以这不会花费很长时间吗?

解决方案

我已经使用 timeit 运行了一些测试,并且(虽然 list 执行速度稍快)有无显着差异:

<预><代码>>>>导入时间>>># 对于集合>>>timeit.timeit("for i in s: pass", "s = set([1,4,7,10,13])")0.20565616500061878>>># 对于列表>>>timeit.timeit("for i in l: pass", "l = [1,4,7,10,13]")0.19532391999928223

即使多次尝试,这些值也几乎保持不变(0.200.19).

但是,创建集合的开销可能巨大.

Say that I have a list of strings and a set of the same strings:

l = [str1, str2, str3, str4, ...]
s = set([str1, str2, st3, str4, ...])

I need to run a string comparison with a phrase that I have: comparephrase

I need to iterate over all the elements in the list or the set and generate a ratio between the comparephrase and the compared string. I know that set() is faster when we are doing a membership test. However, I am not doing a membership test but comparing the phrase that I have and the strings in the list/set. Does set() still offer a faster speed? If so, why? It seems to me that this set is actually a set with a list inside. Wouldn't that take the long time since we're iterating over the list within the set?

解决方案

I've run some tests with timeit, and (while list performs slightly faster) there is no significant difference:

>>> import timeit
>>> # For the set
>>> timeit.timeit("for i in s: pass", "s = set([1,4,7,10,13])")
0.20565616500061878
>>> # For the list
>>> timeit.timeit("for i in l: pass", "l = [1,4,7,10,13]")
0.19532391999928223

These values stay very much the same (0.20 vs. 0.19) even when trying multiple times.

However, the overhead of creating the sets can be significant.

这篇关于哪个更快:迭代集合和迭代列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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