【问题标题】:Which python core type is most efficient for in-list comparisons?哪种 python 核心类型对于列表内比较最有效?
【发布时间】:2020-09-11 04:14:19
【问题描述】:

问题是:在python中,对于inlist比较的参考集,哪种数据结构最有效,即

  • 有一个相对较少项目的参考列表:计数通常在 2 到 20 之间
  • 有任意输入数据,通常是数百万个项目(取决于不同的类实例化)

我想知道参考列表的哪些数据结构最有效,以及是否根据参考列表的长度进行权衡。例如,字典键查找是在恒定时间内完成的,但我认为查找成本与索引列表查找相比是昂贵的,所以我想知道对于 5 项参考列表,列表是否会更有效。

我编写了一个快速程序来测试这一点,因此我将自己回答这个问题,但对其他想法和评论感兴趣。

【问题讨论】:

    标签: python performance processing-efficiency


    【解决方案1】:

    我构建了一个程序来按照以下标准对此进行基准测试。

    tl;dr :在所有配置中,套装始终是最快的或并列最快的。

    故障

    • 我考虑了以下类型的引用集:dict、list、tuple 和 set
    • 我考虑了参考集的大小:2、5、20、50
    • 如果字符串有特殊的效率,我对所有字符串和混合类型(str、float、int、bool)进行了建模。
    • 我还对数据中的所有项目都在参考中并且数据中没有 * 项目在参考列表中的情况进行了建模。

    要点:

    • 令我惊讶的是,在所有场景中,集合都是最有效(或并列)的。
    • 正如预期的那样,对于较长的引用集,列表和元组的效率会越来越低。
    • 在非常小的计数(n=2,n=5)下,列表有时比 dicts 更有效,尤其是当大量项目不匹配时,对于所有字符串。
    • 当项目不匹配时检查 dict 的运行速度始终比匹配时慢,这让我感到惊讶,特别是因为集合没有那么多变化。

    这是在配备 i7 处理器、8gb 内存和 Ubuntu 20.04 的 Lenovo Yoga 910 上使用 Python 3.7 运行的

    
    Summary:
        Len comparison: 2
        Len in: 10000000
        Len out: 10000000
        All strings: True
        Examples: ['@xj?', '@q^f_lv^m']
    
    *** All In ***
        Dict: 0.53 s        (10000000 / 0)
        List: 0.51 s        (10000000 / 0)
        Set: 0.49 s     (10000000 / 0)
        Tuple: 0.51 s       (10000000 / 0)
    
    *** All Out ***
        Dict: 0.82 s        (0 / 10000000)
        List: 0.63 s        (0 / 10000000)
        Set: 0.49 s     (0 / 10000000)
        Tuple: 0.64 s       (0 / 10000000)
    
    
    
    Summary:
        Len comparison: 2
        Len in: 10000000
        Len out: 10000000
        All strings: False
        Examples: [5405, False]
    
    *** All In ***
        Dict: 0.52 s        (10000000 / 0)
        List: 0.51 s        (10000000 / 0)
        Set: 0.49 s     (10000000 / 0)
        Tuple: 0.5 s        (10000000 / 0)
    
    *** All Out ***
        Dict: 0.72 s        (0 / 10000000)
        List: 0.75 s        (0 / 10000000)
        Set: 0.6 s      (0 / 10000000)
        Tuple: 0.78 s       (0 / 10000000)
    
    Summary:
        Len comparison: 5
        Len in: 10000000
        Len out: 10000000
        All strings: True
    
    *** All In ***
        Dict: 0.48 s        (10000000 / 0)
        List: 0.67 s        (10000000 / 0)
        Set: 0.42 s     (10000000 / 0)
        Tuple: 0.65 s       (10000000 / 0)
    
    *** All Out ***
        Dict: 0.92 s        (0 / 10000000)
        List: 0.89 s        (0 / 10000000)
        Set: 0.47 s     (0 / 10000000)
        Tuple: 0.88 s       (0 / 10000000)
    
    Summary:
        Len comparison: 5
        Len in: 10000000
        Len out: 10000000
        All strings: False
        Examples: [394.761846741113, 'ih6ss(*-?n*{3(', 19425, True, 'c4npc*7;#uu|a!%*s5}']
    
    *** All In ***
        Dict: 0.6 s     (10000000 / 0)
        List: 0.79 s        (10000000 / 0)
        Set: 0.53 s     (10000000 / 0)
        Tuple: 0.75 s       (10000000 / 0)
    
    *** All Out ***
        Dict: 0.83 s        (0 / 10000000)
        List: 1.1 s     (0 / 10000000)
        Set: 0.6 s      (0 / 10000000)
        Tuple: 1.1 s        (0 / 10000000)
    
    
    
    Summary:
        Len comparison: 20
        Len in: 10000000
        Len out: 10000000
        All strings: True
    
    *** All In ***
        Dict: 0.5 s     (10000000 / 0)
        List: 1.4 s     (10000000 / 0)
        Set: 0.42 s     (10000000 / 0)
        Tuple: 1.4 s        (10000000 / 0)
    
    *** All Out ***
        Dict: 0.95 s        (0 / 10000000)
        List: 2.2 s     (0 / 10000000)
        Set: 0.46 s     (0 / 10000000)
        Tuple: 2.2 s        (0 / 10000000)
    
    Summary:
        Len comparison: 20
        Len in: 10000000
        Len out: 10000000
        All strings: False
        Examples: [10800, False, True, 2467.0373245526216, -175.42855370588768]
    
    *** All In ***
        Dict: 0.59 s        (10000000 / 0)
        List: 1.4 s     (10000000 / 0)
        Set: 0.54 s     (10000000 / 0)
        Tuple: 1.3 s        (10000000 / 0)
    
    *** All Out ***
        Dict: 0.84 s        (0 / 10000000)
        List: 3.1 s     (0 / 10000000)
        Set: 0.7 s      (0 / 10000000)
        Tuple: 2.9 s        (0 / 10000000)
    
    
    Summary:
        Len comparison: 50
        Len in: 10000000
        Len out: 10000000
        All strings: True
        Examples: ['jz{>-ob*/b^fb)m7sbr&', '[1rs`b`y\\', 'l1--', '(.95', "6z|1!v_*,i_#'q/"]
    
    *** All In ***
        Dict: 0.48 s        (10000000 / 0)
        List: 2.7 s     (10000000 / 0)
        Set: 0.45 s     (10000000 / 0)
        Tuple: 2.7 s        (10000000 / 0)
    
    *** All Out ***
        Dict: 0.85 s        (0 / 10000000)
        List: 4.9 s     (0 / 10000000)
        Set: 0.54 s     (0 / 10000000)
        Tuple: 4.9 s        (0 / 10000000)
    
    Summary:
        Len comparison: 50
        Len in: 10000000
        Len out: 10000000
        All strings: False
        Examples: [False, False, "$zdb+7!bs|5y?-'3__5", False, 814.9305661272301]
    
    *** All In ***
        Dict: 0.59 s        (10000000 / 0)
        List: 2.9 s     (10000000 / 0)
        Set: 0.54 s     (10000000 / 0)
        Tuple: 2.7 s        (10000000 / 0)
    
    *** All Out ***
        Dict: 0.85 s        (0 / 10000000)
        List: 7.0 s     (0 / 10000000)
        Set: 0.63 s     (0 / 10000000)
        Tuple: 6.7 s        (0 / 10000000)
    
    

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多