当前位置:网站首页>Performance comparison between set and list
Performance comparison between set and list
2022-07-28 04:38:00 【transformer_ WSZ】
When the amount of data in the set is particularly large , To determine whether an element is in the set , It is recommended to use set instead of list , The two performances differ greatly . Here's a test :
from tqdm import tqdm
import random
import time
The construction length is length Array of
def construct_data(length):
l = []
for i in range(length):
l.append(i)
random.shuffle(l)
return l, set(l)
test num Time
test list
length, num = int(1e6), int(1e4)
l, s = construct_data(length)
start_l = time.time()
for _ in tqdm(range(num)):
r = random.randint(0, length-1)
if r in l:
pass
end_l = time.time()
print("test list time: {} seconds".format(end_l-start_l))
100%|██████████| 10000/10000 [02:52<00:00, 58.00it/s]
test list time: 172.42421102523804 seconds
test set
start_s = time.time()
for _ in tqdm(range(num)):
r = random.randint(0, length-1)
if r in s:
pass
end_s = time.time()
print("test set time: {} seconds".format(end_s-start_s))
100%|██████████| 10000/10000 [00:00<00:00, 343595.45it/s]
test set time: 0.03251051902770996 seconds
You can see ,set Is really faster than list Much faster . After all set The underlying use hash Hash implementation , To find an element, in theory, just O(1) Time , and list Is the traversal , need O(n) Time . When the data volume is small , There is no difference between the two , The amount of data is a little larger , The gap is very clear .
边栏推荐
- 高数_第4章__曲线积分
- 【sylar】实战篇-基于 redis 的参数查询服务
- Explain initialization list
- 【二、移动web网页开发】2D&3D转换与动画、移动端布局、响应式布局
- Reading the paper "learning span level interactions for aspect sentimental triple extraction"
- transform: failed to synchronize: cudaErrorAssert: device-side assert triggered
- Pyqt based grouping tool
- Go grpc: a solution of connection reset by peer
- [performance optimization methodology series] III. core idea of performance optimization (2)
- 物联网工业串口转WiFi模块 无线路由WiFi模块的选型
猜你喜欢

pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null

Information system project manager (2022) - key content: information system integrated testing and management, project management maturity model, quantitative project management (21)

Information system project manager (2022) - key content: intellectual property rights and standards and specifications (22)

How to select reliable securities analysts?

Efficient practice intelligent analysis of massive log data in aviation industry

Jupyter Notebook安装代码提示功能

Constructor of member function

Idea2022 change the local warehouse and configure Alibaba cloud central warehouse

How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
![[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]](/img/af/27e3794f93166d8ecad9b42dafaf0f.png)
[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]
随机推荐
【YOLOv5实战5】基于YOLOv5的交通标志识别系统-YOLOv5整合PyQt5
Information system project manager (2022) - key content: Project Portfolio Management (19)
[Sylar] framework -chapter14 tcpserver module
网页源代码查看竟然有这么多方法!你都知道吗?
[Sylar] framework chapter -chapter21- environment variable module
【sylar】框架篇-Chapter20-守护进程模块
【sylar】框架篇-Chapter11-Socket 模块
Reading of papers on "towards generative aspect based sentimental analysis"
What to do when encountering slow SQL? (next)
【实战】使用 Web Animations API 实现一个精确计时的时钟
[Sylar] framework -chapter9-hook module
MySQL:数据类型和运算符
Work fishing clock simulator wechat applet source code
Space complexity calculation super full sorting!! (calculation of hand tearing complexity
Blooming old trees -- quickly build a map bed application with imageprocessor
Render the data obtained from the database to the table in elementui
Information system project manager (2022) - key content: information system integrated testing and management, project management maturity model, quantitative project management (21)
【sylar】实战篇-基于 redis 的参数查询服务
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
Important SQL server functions - date functions