-
Notifications
You must be signed in to change notification settings - Fork 958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next() 函数提高性能的疑惑 #43
Comments
你好 @klmjoi 。 使用“列表表达式”和 “next + 生成器表达式“这两种方法最大的区别在于 何时中断,对于前者来说,无论找没找到,都会遍历完整个列表。而后者在找到第一个元素就会返回。 如果用时间复杂度
如果要对比二者的效率,可以选一个规模稍微大点的 list: # 构造一个 1 到 999 的列表,找里面第一个可以被 200 整除的数,也就是 200
In [3]: numbers = list(range(1, 1000))
# 使用列表表达式,要搜完整个列表,大概耗费 40 µs
In [4]: %timeit [i for i in numbers if i % 200 == 0][0]
43 µs ± 2.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: %timeit [i for i in numbers if i % 200 == 0][0]
40.8 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
### 使用 next,找到第一个就返回,大概耗费 9µs
In [10]: %timeit next((i for i in numbers if i % 200 == 0))
8.61 µs ± 83.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [11]: %timeit next((i for i in numbers if i % 200 == 0))
9.24 µs ± 339 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 为什么你的例子里使用 next() 更慢?主要原因是源列表太短,使用 next() 少做的那几次检索还抵不上多调用一次 next 的开销。 感谢你的留言,我在以后修订文章时会补一些这部分的内容。 |
确实如此,感谢~ |
在大数据的情况下,这个数列是以百万个为单位,省的时间就更加明显了。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @piglei , 您的系列文章让人收益匪浅。
读到第四章“容器的门道”,对 next() 可以高效的实现 “从列表中查找第一个满足条件的成员”,我自己测了一下性能,似乎还不如普通方法。
The text was updated successfully, but these errors were encountered: