结论:

操作队尾元素时,速度差可忽略不计;

操作队头元素时,Deque远快于List

from collections import deque
from time import timedef test(num, struct, fun):sta = time()for _ in range(num):fun(struct)end = time()print("time cost: ", end - sta)

1 队尾插入元素---速度对比

num = int(1e7)
struct = list()
fun = lambda struct: struct.append(1)
test(num, struct, fun)time cost:  0.7700014114379883
num = int(1e7)
struct = deque()
fun = lambda struct: struct.append(1)
test(num, struct, fun)time cost:  0.6693985462188721

2 队尾弹出元素---速度对比

num = int(1e7)
struct = list(i for i in range(num))
fun = lambda struct: struct.pop()
test(num, struct, fun)time cost:  0.8014125823974609
num = int(1e7)
struct = deque(i for i in range(num))
fun = lambda struct: struct.pop()
test(num, struct, fun)time cost:  0.6997454166412354

3 队头插入元素---速度对比

num = int(1e5)
struct = list()
fun = lambda struct: struct.insert(0, 1)
test(num, struct, fun)time cost:  1.4022290706634521
num = int(1e5)
struct = deque()
fun = lambda struct: struct.appendleft(1)
test(num, struct, fun)time cost:  0.012262105941772461
num = int(1e5)
struct = deque()
fun = lambda struct: struct.insert(0, 1)
test(num, struct, fun)time cost:  0.00999760627746582

4 队头弹出元素---速度对比

num = int(1e5)
struct = list(i for i in range(num))
fun = lambda struct: struct.pop(0)
test(num, struct, fun)time cost:  9.153056383132935
num = int(1e5)
struct = deque(i for i in range(num))
fun = lambda struct: struct.popleft()
test(num, struct, fun)time cost:  0.007311344146728516

Python | List和Deque的速度对比相关推荐

  1. python deque_python中deque类详解

    最近在pythonTip做题的时候,遇到了deque类,以前对其不太了解,现在特此总结一下 deque类是python标准库collections模块中的一项,它提供了两端都可以操作的序列,这意味着, ...

  2. Python容器专题 - deque(队列)--双向队列对象

    deque(队列)–双向队列对象 Deque队列是由栈或者queue队列生成的.列表也可以用作队列,其中先添加的元素被最先取出 ("先进先出"):普通列表的一个巨大缺陷在于,其往开 ...

  3. python队列(deque)

    collections.deque介绍 collections 是 python 内建的一个集合模块,里面封装了许多集合类,其中队列相关的集合只有一个:deque. deque 是双边队列(doubl ...

  4. Cython,Python,C/C++的运行速度对比

    关于Cython,Python,C/C++的运行速度的对比 测试环境: 处理器: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx 2.10 GHz 机带: ...

  5. maya python 开根号,基于python不同开根号的速度对比分析

    我就废话不多说了,大家还是直接看代码吧~ import time import math import numpy as np def timeit1(): s = time.time() for i ...

  6. python deque双端队列的神奇用法

    python中的deque双端队列,类似list的任意一端都可实现较快的add和pop操作 from collections import dequed=deque(maxlen=20) for i ...

  7. 【上篇】Python实现最短路问题常见求解算法——Label Correcting Algorithm(deque)

    基于python语言,实现deque label correcting 算法对最短路问题(Shortest Path Problem)进行求解. 目录 1. 适用场景 2. 算法说明 3. 测试网络 ...

  8. Python实现最短路问题常见求解算法1——Label Correcting Algorithm(deque)

    原文链接 1. 适用场景 无负环网络 起点与终点间至少存在一条可行路径 可求解单一起点到多点的最短路径 2. 算法说明 应用标号法求解最短路问题时有两种选择,一是采用 label setting 策略 ...

  9. Python编程规范及性能优化

    为什么80%的码农都做不了架构师?>>>    Ptyhon编程规范 编码 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- .设置编辑器 ...

最新文章

  1. 自旋锁spinlock解析
  2. UEFI+GPT安装windows
  3. 电量检测芯片BQ27510使用心得
  4. es过滤html标签,Elasticsearch 分词器
  5. 40. Combination Sum II 组合总和 II
  6. NHibernate学习笔记(转载):many-to-one/one-to-many/many-to-many关系映射
  7. Java入门算法(树篇)
  8. python 基础篇(一)--linux命令篇
  9. php mongodb长连接吗,PHP - MongoDB连接攻略
  10. 货币php是什么,php做什么的【货币问答】- php做什么的所有答案 - 联合货币
  11. 阿里云ODPS升级为一体化大数据平台 满足用户多元化数据计算需求
  12. 白杨SEO:流量红利消失,现在都在各渠道做推广,我们还有必要做官方网站吗?怎么做呢?
  13. 吴恩达机器学习入门笔记12/13-聚类与降维
  14. win10——戴尔笔记本电脑插上耳机没有声音电脑外放
  15. Android中Bitmap的分析与使用
  16. flink catalog 及dialect、数据转存分析
  17. 使用Sharding-Jdbc进行数据拆分
  18. 船舶航速优化文献阅读
  19. linux yum安装erlang,在 CentOS 5.7 上通过 YUM 安装 Erlang 过程
  20. TensorFlow v2.0实现Word2Vec算法

热门文章

  1. MySQL基本数据类型与Java基本数据类型
  2. SpringBoot项目访问jsp页面500问题处理
  3. c++/c中的预编译,文件包含伪指令,#include,包含哨卫,头文件保护
  4. zookeeper C API 完整运行实例
  5. Linux:shell 脚本 自动解压压缩文件tar.gz到指定目录
  6. 加速想象力 AR/VR 训练营(无锡站)签约挂牌仪式成功举行
  7. 转:长篇小说《七月七日晴》(超感人的)(续)
  8. 《定风波》--苏轼之我最喜欢的一首词
  9. 函数的重载与重载解析
  10. 2022-01-18 任何的长相厮守、不离不弃都是术业有专攻的基础。遇见3D MAX只叹恨晚。。。