python-stack
implements
list
deque
LifoQueue
Argue
list
This works great for several operations, like indexing into the list. Getting myList[3] is fast, as Python knows exactly where to look in memory to find it. This memory layout also allows slices to work well on lists.
The contiguous memory layout is the reason that list might need to take more time to .append() some objects than others. If the block of contiguous memory is full, then it will need to get another block, which can take much longer than a normal .append():
If the block of contiguous memory is full, then it will need to get another block, which can take much longer than a normal .append():
如果连续的内存块被写满,再加入新元素会导致重新开辟内存,耗时增加。
list 线程不安全
deque
- deque, on the other hand, is built upon a doubly linked list. In a linked list structure, each entry is stored in its own memory block and has a reference to the next entry in the list.
- A doubly linked list is just the same, except that each entry has references to both the previous and the next entry in the list. This allows you to easily add nodes to either end of the list.
- deque 使用了双向链表维护内存,继而可以灵活方便的进行增加元素的操作
LifoQueue
Unlike deque, LifoQueue is designed to be fully thread-safe. All of its methods are safe to use in a threaded environment. It also adds optional time-outs to its operations which can frequently be a must-have feature in threaded programs.
This full thread safety comes at a cost, however. To achieve this thread-safety, LifoQueue has to do a little extra work on each operation, meaning that it will take a little longer.
Frequently, this slight slow down will not matter to your overall program speed, but if you’ve measured your performance and discovered that your stack operations are the bottleneck, then carefully switching to a deque might be worth doing.
LifoQueue 线程安全,但性能会有些许损失,一般情况下影响不大。但如果发现栈操作成为了瓶颈,切换为deque要小心,可能会导致问题
Conclusion
- Python Stacks: Which Implementation Should You Use?
In general, you should use a deque if you’re not using threading. If you are using threading, then you should use a LifoQueue unless you’ve measured your performance and found that a small boost in speed for pushing and popping will make enough difference to warrant the maintenance risks.
list may be familiar, but it should be avoided because it can potentially have memory reallocation issues. The interfaces for deque and list are identical, and deque doesn’t have these issues, which makes deque the best choice for your non-threaded Python stack.
You now know what a stack is and have seen situations where they can be used in real-life programs. You’ve evaluated three different options for implementing stacks and seen that deque is a great choice for non-threaded programs. If you’re implementing a stack in a threading environment, then it’s likely a good idea to use a LifoQueue.
单线程场景下deque是推荐选择,多线程场景下,请选择LifoQueue。
python-stack的更多相关文章
- Convert SVG to PNG in Python - Stack Overflow
Convert SVG to PNG in Python - Stack Overflow Convert SVG to PNG in Python
- How to convert `ctime` to `datetime` in Python? - Stack Overflow
How to convert `ctime` to `datetime` in Python? - Stack Overflow How to convert `ctime` to `datetime ...
- The MEAN stack is a modern replacement for the LAMP (Linux, Apache, MySQL, PHP/Python) stack
w https://www.mongodb.com/blog/post/building-your-first-application-mongodb-creating-rest-api-using- ...
- Python之异常追踪模块:traceback
正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...
- DTrace patch for Python 2.7.x and 3.x
DTrace patch for Python 2.7.x and 3.x Última Actualización: 21 de septiembre de 2015 https://www.jce ...
- Python: import vs from (module) import function(class) 的理解
Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...
- python数据结构之栈
栈 栈(stack),有些地方称为堆栈,是一种容器,可存入数据元素.访问元素.删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top)进行加入数据(英语:push)和输出数据(英语: ...
- Python Twisted系列教程3:初步认识Twisted
作者:dave@http://krondo.com/our-eye-beams-begin-to-twist/ 译者:杨晓伟(采用意译) 可以从这里从头开始阅读这个系列. 用twisted的方式实现前 ...
- 【380】python 获取列表排序后的索引列表
参考:Equivalent of Numpy.argsort() in basic python? - Stack Overflow 通过 enumerate 实现 [i for i,v in sor ...
- 在 Python 中使用 GDB 来调试 转载
2013/11/01 | Comments 大约一年前,我接触了 Java 中的 Btrace 能够不停机查看线上 JVM 运行情况的特性让我艳羡不已. 另外还有强悍的 jStack 和 jConso ...
随机推荐
- Spring Boot 整合邮件服务
参考教程 首先参考了 Spring Boot整合邮件配置,这篇文章写的很好,按照上面的操作一步步走下去就行了. 遇到的问题 版本配置 然后因为反复配置版本很麻烦,所以参考了 如何统一引入 Spring ...
- python自产调试工具pdb的使用
python自产调试工具pdb的使用 介绍 调试打印在写代码的时候不可避免 项目越大,调试可能花的时间会越多 print调试可能是最早用的,一段时间内你都会习惯这种方式 一旦成了老鸟,你应该会去用ID ...
- 2022-12-12:有n个城市,城市从0到n-1进行编号。小美最初住在k号城市中 在接下来的m天里,小美每天会收到一个任务 她可以选择完成当天的任务或者放弃该任务 第i天的任务需要在ci号城市完成,
2022-12-12:有n个城市,城市从0到n-1进行编号.小美最初住在k号城市中 在接下来的m天里,小美每天会收到一个任务 她可以选择完成当天的任务或者放弃该任务 第i天的任务需要在ci号城市完成, ...
- Midjourney|文心一格prompt教程[Text Prompt(上篇)]:品牌log、App、徽章、插画、头像场景生成,各种风格选择:科技风、运动风
Midjourney|文心一格prompt教程[Text Prompt(上篇)]:品牌log.App.徽章.插画.头像场景生成,各种风格选择:科技风.运动风 1.撰写 Text Prompt 注意事项 ...
- 《数据结构(C语言版)》严蔚敏代码实现———顺序表
一.前言 最近在重新学习数据结构啦,网上说这本书挺不错哒,于是我开始啃这本书咯...有一说一,严奶奶的书挺好的,就是有点大量使用指针...需要沉下心来,看一看画一画才能懂,我自己手敲了一遍书上代码,加 ...
- Doris(一) -- 简介和安装
Doris 简介 Doris 概述 Apache Doris 由百度大数据部研发 (之前叫百度 Palo,2018 年贡献到 Apache 社区后,更名为 Doris), 在百度内部,有超过 200 ...
- 6.4. HttpClient
1. 什么是HttpClient? HttpClient是Java 11中引入的一个新特性,用于支持同步和异步发送HTTP请求以及处理HTTP响应.它提供了简单易用的API,使得发送HTTP请求变得非 ...
- ffuf的使用
ffuf:模糊测试 使用 ffuf 进行枚举.模糊测试和目录暴力破解 安装 https://github.com/ffuf/ffuf 建议:https://github.com/danielmiess ...
- 你还在用Object.equals()方法吗?
前言 当<阿里巴巴Java开发手册>发布后,我也是仔细进行了阅读,想从中找出一些"标准",让自己的代码质量提高.手册中对 Object 的 equals 方法的使用进行 ...
- EF Core + MySQL 基本增删改查
前言 基于EF Core + MySQL的基本增删改查,示例是基于.NET6 + EF Core + MySQL 创建实体和数据库.EFCore 数据迁移项目基础上的内容增加.同时也是对基于Canal ...