当前位置:网站首页>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 .
边栏推荐
- [Sylar] framework -chapter24- support business modularization
- 25 openwrt guest network add
- Go grpc: a solution of connection reset by peer
- 重要的 SQL Server 函数 - 数字函数
- Information system project manager (2022) - key content: Project Portfolio Management (19)
- 空间复杂度计算超全整理!!(一起手撕复杂度计算
- [practice] use the web animations API to realize a clock with accurate timing
- Reading of papers on "towards generative aspect based sentimental analysis"
- np. The data returned from delete details is the data after deleting the specified dimension
- [Sylar] framework -chapter12 bytearray module
猜你喜欢

Important SQL server functions - date functions

Study of the Intel aria 10 Avalon mm DMA interface for PCI Express solutions User Guide

MySQL: data types and operators

2022-7-27 顾宇佳 学习笔记

Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers

Practice and thinking of AI standardization engine in pink client

Reading of a unified generic framework for aspect based sentimental analysis
![[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]](/img/fd/6b261670c12e4d89c27364bcdf2a02.jpg)
[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]

Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation

MySQL:数据类型和运算符
随机推荐
MySQL数据库————初识数据库
Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
Destructor of member function
Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation
Esp8266 WiFi module and mobile communication
Important SQL server functions - date functions
The unsatisfied analysis of setup and hold timing is the solution
If mongoose exists, it will be updated; if it does not exist, it will be added
Information system project manager (2022) - key content: Knowledge Management (15)
[Sylar] framework chapter -chapter21- environment variable module
方舟生存进化自建服务器要多少成本?
MySQL:数据类型和运算符
Go grpc: a solution of connection reset by peer
21 openwrt kernel module changed to.Ko automatic loading
Select sorting method
pytorch打包exe出现WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI13
【sylar】框架篇-Chapter12-ByteArray 模块
Password key hard coding check
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
Transformer landing | next vit realizes the real-time landing of industrial tensorrt, surpassing RESNET and cswin