RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1027473
Accepted
Echobana
Echobana
Asked:2020-09-24 16:04:39 +0000 UTC2020-09-24 16:04:39 +0000 UTC 2020-09-24 16:04:39 +0000 UTC

列表、集合还是生成器?

  • 772

显然,例如,要检查集合中的存在,使用 set 比使用 list 更有利可图,也就是说,a in [i*i for i in range(n)]不如a in {i*i for i in range(n)}. 但是使用发电机要优化多少a in (i*i for i in range(n))呢?内存被保存了,还有更多?或者我不太明白如何检查生成器中是否存在元素。

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    insolor
    2020-09-24T21:50:11Z2020-09-24T21:50:11Z

    理论上,如果检查将执行一次,那么最好使用生成器,因为 在这种情况下,将仅通过序列到达所需元素(或到序列末尾,如果该元素不在其中)。要创建set或list解释,有必要绕过整个元素集一次,然后搜索它。

    如果检查将执行多次(例如,在循环中),那么最好提前创建一个现成的set,然后检查 this 中已经存在的元素set。

    以供参考:

    • 列表或生成器搜索是线性搜索(即,只是元素的顺序迭代,直到找到所需的元素),搜索时间与序列的长度成正比 ( O(n))
    • Python 中的集合被实现为哈希表,查找时间(平均)不依赖于元素的数量,它总是大致相同(О(1))。但是由于 哈希表的内部结构比列表更复杂,创建集合需要更多时间。

    这是一个理论,在实践中,在没有先保存到变量的情况下进行搜索时,我们得到以下结果:

    无需准备

    我们看到列表搜索结果比生成器搜索稍快。很可能是因为在生成器中获取下一个元素比在列表中花费更多的时间。


    为了评估预处理搜索的实际性能,我们编写了以下程序:

    n = 10000000
    a = (n-1)**2  # худший случай для линейного поиска
    
    s_list = [i*i for i in range(n)]
    s_set = {i*i for i in range(n)}
    s_gen = (i*i for i in range(n))
    
    def in_list():
        return a in s_list
    
    def in_set():
        return a in s_set
    
    def in_gen():
        return a in s_gen
    
    print(in_list())
    print(in_set())
    print(in_gen())
    

    使用探查器运行:

    python3 -m cProfile -s time test.py
    

    结果:

    True
    True
    True
             10000011 function calls in 3.927 seconds
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    1.976    1.976    1.976    1.976 test.py:5(<setcomp>)
     10000000    0.768    0.000    0.768    0.000 test.py:6(<genexpr>)
            1    0.625    0.625    1.393    1.393 test.py:14(in_gen)
            1    0.489    0.489    0.489    0.489 test.py:4(<listcomp>)
            1    0.069    0.069    0.069    0.069 test.py:8(in_list)
            3    0.000    0.000    0.000    0.000 {built-in method builtins.print}
            1    0.000    0.000    3.927    3.927 test.py:1(<module>)
            1    0.000    0.000    3.927    3.927 {built-in method builtins.exec}
            1    0.000    0.000    0.000    0.000 test.py:11(in_set)
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    

    我们看到,就创建时间而言,集合 ( <setcomp>) 的创建速度最慢,然后是生成器 ( <genexpr>),然后是列表 ( <listcomp>)。

    按搜索时间:最慢 - 按生成器搜索 ( in_gen),快一点 - 按列表 ( in_list),最快 - 在集合中搜索 ( in_set)。

    口译员:

    Python 3.6.8 (default, Aug 20 2019, 17:12:48) 
    [GCC 8.3.0] on linux
    

    结果:

    • 在不先保存到变量的情况下进行搜索时,最好使用列表。但是,如果有内存限制,最好使用生成器。
    • 使用 prestore 搜索时,最好使用 set
    • 7

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5