implements

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的更多相关文章

  1. Convert SVG to PNG in Python - Stack Overflow

    Convert SVG to PNG in Python - Stack Overflow Convert SVG to PNG in Python

  2. 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 ...

  3. 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- ...

  4. Python之异常追踪模块:traceback

    正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...

  5. 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 ...

  6. Python: import vs from (module) import function(class) 的理解

    Python: Import vs From (module) import function(class) 本文涉及的 Python 基本概念: Module Class import from . ...

  7. python数据结构之栈

    栈 栈(stack),有些地方称为堆栈,是一种容器,可存入数据元素.访问元素.删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top)进行加入数据(英语:push)和输出数据(英语: ...

  8. Python Twisted系列教程3:初步认识Twisted

    作者:dave@http://krondo.com/our-eye-beams-begin-to-twist/ 译者:杨晓伟(采用意译) 可以从这里从头开始阅读这个系列. 用twisted的方式实现前 ...

  9. 【380】python 获取列表排序后的索引列表

    参考:Equivalent of Numpy.argsort() in basic python? - Stack Overflow 通过 enumerate 实现 [i for i,v in sor ...

  10. 在 Python 中使用 GDB 来调试 转载

    2013/11/01 | Comments 大约一年前,我接触了 Java 中的 Btrace 能够不停机查看线上 JVM 运行情况的特性让我艳羡不已. 另外还有强悍的 jStack 和 jConso ...

随机推荐

  1. dp杂题选做

    树的数量 题目其实挺简单的,难点在于状态的设计(其实也没多难). 令 \(f_i\) 表示 \(i\) 个点的 \(m\) 叉树的数量,发现无法转移.设 \(g_{i,j}\) 表示根节点所在子树内有 ...

  2. Java中数字相关的类有哪些?Nuber数字类和Math数学类详解

    前言 我们在解决实际问题时,会经常对数字.日期和系统设置进行处理,比如在我们的代码中,经常会遇到一些数字&数学问题.随机数问题.日期问题和系统设置问题等. 为了解决这些问题,Java给我们提供 ...

  3. 文心一言 VS chatgpt (2)-- 算法导论1.2

    一.给出在应用层需要算法内容的应用的一个例子,并讨论涉及的算法的功能. 文心一言: 在应用层需要算法内容的应用的一个例子是无人机自主飞行控制.无人机自主飞行控制需要算法来确定无人机的位置.速度和方向, ...

  4. 2021-11-04:计算右侧小于当前元素的个数。给你`一个整数数组 nums ,按要求返回一个新数组 counts 。数组 counts 有该性质: counts[i] 的值是 nums[i] 右

    2021-11-04:计算右侧小于当前元素的个数.给你`一个整数数组 nums ,按要求返回一个新数组 counts .数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧 ...

  5. Django-2:创建项目Project

    命令:django-admin startproject mysite PS C:\Users\liujun> cd e:\pyapp\cmdbPS E:\pyapp\cmdb> djan ...

  6. 使用ONE.Abp快速开发微服务,再也不用加班了

    项目背景 公司采用项目制工作方式,因此在不同项目上可能存在多个团队开发独立的代码库,但通用的基础设施却是相同的,这可能导致每个项目都需要编写相同的代码,并重复造轮子.更严重的是,每个项目都有自己的用户 ...

  7. PHP代码审计——ThinkPHP基础

    一.ThinkPHP概述 1. ThinPHP是一个轻量级的PHP框架,旨在提供快速开发Web应用程序的工具和资源.它采用了MVC(Model-View-Controller)架构,使开发人员可以更好 ...

  8. AcWing 1019. 庆功会

    为了庆贺班级在校运动会上取得全校第一名成绩,班主任决定开一场庆功会,为此拨款购买奖品犒劳运动员. 期望拨款金额能购买最大价值的奖品,可以补充他们的精力和体力. 输入格式 第一行二个数n,m,其中n代表 ...

  9. AcWing 423. 采药

    辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师. 医师为了判断他的资质,给他出了一个难题. 医师把他带到一个到处都是草药的山洞里对他说:"孩子 ...

  10. Netty实战(三)

    目录 一.Channel.EventLoop 和 ChannelFuture 1.1 Channel 接口 1.2 EventLoop 接口 1.3 ChannelFuture 接口 二.Channe ...