哪个python核心类型对于列表内比较最有效?

问题描述 投票:0回答:1

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

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

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

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

python performance processing-efficiency
1个回答
0
投票

我建立了一个程序来根据以下标准对此进行基准测试。

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

故障

  • 我考虑了以下类型的参考集:字典,列表,元组和集合
  • 我考虑了以下参考集的大小:2、5、20、50
  • 如果字符串有特殊的效率,我会为所有字符串建模,并混合均匀划分的类型(str,float,int,bool)。
  • 我还对以下情况进行了建模:数据中的所有项目都在参考文献中,而数据中的所有*项目都不在参考文献列表中。

外卖:

  • 令我惊讶的是,布景在每种情况下都是最有效的(或并列的。)>
  • 正如预期的那样,对于较长的参考集,列表和元组的效率越来越低。
  • 在非常少的计数(n = 2,n = 5)下,列表有时比字典更有效,尤其是对于大量项目不匹配的所有字符串。
  • 当项目不匹配时检查dict的速度始终比匹配时慢,这让我感到惊讶,特别是因为集合的变化不大。
  • [这是在Python 3.7上,在装有i7处理器,8GB ram和Ubuntu 20.04的Lenovo Yoga 910上运行的


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)

© www.soinside.com 2019 - 2024. All rights reserved.