问题是:在python中,哪种数据结构对于inlist比较的参考集最有效,即
我想知道参考列表的哪种数据结构最有效,以及是否根据参考列表的长度进行权衡。例如,一个dict键查找是在恒定时间内完成的,但是我认为与索引列表查找相比,查找成本昂贵,因此我想知道对于5项引用列表来说,该列表是否会更有效。
我写了一个快速的程序来测试这一点,所以我自己回答这个问题,但对其他想法和评论感兴趣。
我建立了一个程序来根据以下标准对此进行基准测试。
tl; dr:在所有配置中,集合始终是最快的或并列最快的。
[这是在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)