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 ...
随机推荐
- 2023-04-15:ffmpeg的filter_audio.c的功能是生成一个正弦波音频,然后通过简单的滤镜链,最后输出数据的MD5校验和。请用go语言改写。
2023-04-15:ffmpeg的filter_audio.c的功能是生成一个正弦波音频,然后通过简单的滤镜链,最后输出数据的MD5校验和.请用go语言改写. 答案2023-04-15: 代码见gi ...
- 2023-01-14:给定一个二维数组map,代表一个餐厅,其中只有0、1两种值 map[i][j] == 0 表示(i,j)位置是空座 map[i][j] == 1 表示(i,j)位置坐了人 根据防
2023-01-14:给定一个二维数组map,代表一个餐厅,其中只有0.1两种值 map[i][j] == 0 表示(i,j)位置是空座 map[i][j] == 1 表示(i,j)位置坐了人 根据防 ...
- 2023-01-03:超过5名学生的课。编写一个SQL查询来报告 至少有5个学生 的所有班级,返回结果不限顺序。请问sql语句如何写? +---------+ | class | +-----
2023-01-03:超过5名学生的课.编写一个SQL查询来报告 至少有5个学生 的所有班级,返回结果不限顺序.请问sql语句如何写? ±--------+ | class | ±--------+ ...
- 2021-09-21:给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。如果数组中不存在目标值 target,返回 [-1, -1]。要
2021-09-21:给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置.如果数组中不存在目标值 target,返回 [-1, -1].要 ...
- SQL Server 2014 英文版安装教程
安装过程如下 1. 点击setup开始安装. 2. 选择如下的全新安装. 3. 自动生成产品密钥,然后点击下一步. 4. 勾选接受条款,然后点击下一步. 5. 自动更新根据实际情况进行选择,点击下一步 ...
- CMD 常用命令总结
CMD 常用命令总结 小技巧: 输入 help,查看帮助: Tab 键,自动补全: 上/下方向键,查看历史命令: 右键窗口标题栏 -> 属性,可以修改外观样式. # 关机.重启.注销.休眠.定时 ...
- SICP:惰性求值、流和尾递归(Python实现)
求值器完整实现代码我已经上传到了GitHub仓库:TinySCM,感兴趣的童鞋可以前往查看.这里顺便强烈推荐UC Berkeley的同名课程CS 61A. 即使在变化中,它也丝毫未变. --赫拉克利特 ...
- Linux(redhat)镜像
作为一个合格的程序猿,Linux那就是必须得会玩哟呵,搜集了一些镜像分享大家,望笑纳. 云盘地址https://pan.baidu.com/s/1cB-llYI5RdRm9xJDmjFoWg 提取码 ...
- 代码随想录算法训练营Day40 动态规划
代码随想录算法训练营 代码随想录算法训练营Day40 动态规划| 343. 整数拆分 96.不同的二叉搜索树 343. 整数拆分 题目链接:343. 整数拆分 给定一个正整数 n,将其拆分为至少两个正 ...
- PE 结构的三种地址
三种地址 (一)VA(虚拟地址):PE 文件被 Windows 加载到内存后的地址. (二) RVA(相对虚拟地址):PE 文件虚拟地址相对于映射基地址(对于 EXE 文件来说,映射基地址是 ...