我构建了一个程序来按照以下标准对此进行基准测试。
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)