当前位置:网站首页>set与list性能对比
set与list性能对比
2022-07-28 04:37:00 【transformer_WSZ】
当集合中的数据量特别大时,要判断一个元素是否在该集合中,建议使用 set 而不是 list ,两种性能差异非常大。下面做一个测试:
from tqdm import tqdm
import random
import time
构造长度为 length 的数组
def construct_data(length):
l = []
for i in range(length):
l.append(i)
random.shuffle(l)
return l, set(l)
测试 num 次
测试 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
测试 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
可以看到,set 的速度实在比 list 快很多。毕竟 set 底层用hash散列实现,查找一个元素理论上只需 O(1) 时间,而 list 则是遍历,需要 O(n) 时间。数据量小的时候,两者看不出差距,数据量稍微大点,差距非常明显。
边栏推荐
- [Sylar] framework -chapter14 tcpserver module
- Shanghai Telecom released public computing services and signed the action plan of "Joint Innovation Center for intelligent computing applications" with Huawei and other partners
- [mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
- Warning: file already exists but should not: c:\users\workmai\appdata\local\temp appears when Python packages exe\_ MEI13
- Jupyter notebook installation code prompt function
- mysql分区表改造
- [Sylar] framework -chapter7-io coordination scheduling module
- Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers
- 【YOLOv5实战5】基于YOLOv5的交通标志识别系统-YOLOv5整合PyQt5
- 高数_第4章__曲线积分_习题解法
猜你喜欢

Use Baidu developer tool 4.0 to build a dedicated applet IDE

Reading of the paper "attentional encoder network for targeted sentimental classification"
![[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]](/img/5e/dfe029490183ee74687606941ce98e.jpg)
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]

Reading of a unified generic framework for aspect based sentimental analysis

High number_ Chapter 4__ curvilinear integral

VAE generation model (with VAE implementation MNIST code)

Cloud native Devops status survey questionnaire solicitation: kodelurover launched jointly with oschina

Select sorting method

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

Some personal understandings of openpose
随机推荐
重要的 SQL Server 函数 - 字符串实用程序
Information system project manager (2022) - key content: Project Procurement Management (12)
Campus stray cat information recording and sharing applet source code
Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation
Power consumption: leakage power
MySQL: data types and operators
重要的 SQL Server 函数 - 日期函数
高数_第4章__曲线积分
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
[Sylar] framework Chapter 23 summary of module chapter
transform: failed to synchronize: cudaErrorAssert: device-side assert triggered
What is the account opening process of qiniu business school? Is it safe?
Information system project manager (2022) - key content: Project Portfolio Management (19)
[Sylar] framework Chapter 22 auxiliary module
Information system project manager (2022) - key content: Strategic Management (17)
Odoo action analysis (action.client, action.act_window, action.server)
When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
01 node express system framework construction (express generator)
【sylar】框架篇-Chapter8-定时器模块