关于deque
起因是我想做一个「手气不错」的功能,为了提高性能,打算用队列实现,偶然在Stack Overflow看到一个讨论「Efficiency of using a Python list as a queue」
python的list有pop方法,可以实现队列的取出功能,不过据说性能一般般,高赞回答说的
❝You won't run out of memory using the list implementation, but performance will be poor.
❞
那我还是用deque吧,这个deque还是线程安全的,具体的实现先不管了,有空看看它是如何实现的,附官网介绍:
❝Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
❞
使用方法
感觉照搬文档好啰嗦啊…… 使用起来和lsit差不多,不过注意一下list只能在最右边append和pop,而deque可以在左边appendleft和popleft,时间复杂度还是O(1),很舒服~
参考资料
- 官方文档:https://docs.python.org/3/library/collections.html#collections.deque
- https://stackoverflow.com/questions/1296511/efficiency-of-using-a-python-list-as-a-queue
- https://zhuanlan.zhihu.com/p/32201189
![e4edd1be4764af5e06011e3cbb7ce0ac.png](https://img-blog.csdnimg.cn/img_convert/e4edd1be4764af5e06011e3cbb7ce0ac.png)
![70742fd36536fdb0f93720a1dac7b9a9.png](https://img-blog.csdnimg.cn/img_convert/70742fd36536fdb0f93720a1dac7b9a9.png)
![f765cf8311d8aff371688f3a9ae18d47.png](https://img-blog.csdnimg.cn/img_convert/f765cf8311d8aff371688f3a9ae18d47.png)
扫码关注