当前位置:网站首页>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 .
边栏推荐
- MySQL数据库————初识数据库
- could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
- [II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
- Work fishing clock simulator wechat applet source code
- 物联网工业串口转WiFi模块 无线路由WiFi模块的选型
- [Sylar] practical part - redis based parameter query service
- [Niuke] find 1+2+3+... +n
- 网页源代码查看竟然有这么多方法!你都知道吗?
- [Sylar] framework -chapter9-hook module
- Esp8266 WiFi module and mobile communication
猜你喜欢

Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers

Phpstorm2022 connect to the database

Reading the paper "learning span level interactions for aspect sentimental triple extraction"

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

Reading of a unified generic framework for aspect based sentimental analysis

Important SQL server functions - date functions

Campus stray cat information recording and sharing applet source code

【实战】使用 Web Animations API 实现一个精确计时的时钟

Mac installs mysql5.7 through brew

Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers
随机推荐
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance
【sylar】框架篇-Chapter21-环境变量模块
Thoroughly understand the sharing function in wechat games
Information system project manager (2022) - key content: intellectual property rights and standards and specifications (22)
[practice] use the web animations API to realize a clock with accurate timing
High number_ Chapter 4__ Curvilinear integral_ Exercise solution
【sylar】框架篇-Chapter6-协程调度模块
【sylar】框架篇-Chapter8-定时器模块
Power consumption: leakage power
【sylar】框架篇-Chapter20-守护进程模块
Reading the paper "learning span level interactions for aspect sentimental triple extraction"
[Sylar] framework Chapter 22 auxiliary module
Efficient practice intelligent analysis of massive log data in aviation industry
重要的 SQL Server 函数 - 日期函数
could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
Go grpc: a solution of connection reset by peer
printf()打印char* str
Password key hard coding check
Seamless support for hugging face community, colossal AI low-cost and easy acceleration of large model