当前位置:网站首页>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) 时间。数据量小的时候,两者看不出差距,数据量稍微大点,差距非常明显。
边栏推荐
- Idea start project MVN command terminal cannot recognize "MVN" item as cmdlet
- CMake使用基础汇总
- Explain initialization list
- Information system project manager (2022) - key content: Strategic Management (17)
- Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance
- Work fishing clock simulator wechat applet source code
- 【牛客】求1+2+3+...+n
- [Sylar] framework -chapter9-hook module
- [practice] use the web animations API to realize a clock with accurate timing
- Cmake usage base summary
猜你喜欢

Information system project manager (2022) - key content: Project Portfolio Management (19)

物联网工业串口转WiFi模块 无线路由WiFi模块的选型

上班摸鱼打卡模拟器微信小程序源码

Important SQL server functions - numeric functions

重要的 SQL Server 函数 - 日期函数

Render the data obtained from the database to the table in elementui

Jupyter Notebook安装代码提示功能

校园流浪猫信息记录和分享的小程序源码

Important SQL server functions - string utilities

Reading of the paper "attentional encoder network for targeted sentimental classification"
随机推荐
Information system project manager (2022) - key content: Project Portfolio Management (19)
【Oracle】083错题集
C语言初阶——循环语句(while,for,do while)
Information system project manager (2022) - key content: Knowledge Management (15)
[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2
2022-7-27 顾宇佳 学习笔记
Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance
Cmake usage base summary
Jupyter notebook installation code prompt function
Information system project manager (2022) - key content: Information System Security Management (20)
Odoo action analysis (action.client, action.act_window, action.server)
Reading the paper "learning span level interactions for aspect sentimental triple extraction"
After login, the upper right corner changes to enter the login status
重要的 SQL Server 函数 - 日期函数
【实战】使用 Web Animations API 实现一个精确计时的时钟
上班摸鱼打卡模拟器微信小程序源码
[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
Sort - cardinal sort
Phpstorm2022 connect to the database