time complexity of random access in deque in Python [duplicate]

你离开我真会死。 提交于 2019-12-10 18:52:45

问题


I am wondering about the time complexity of the get operation of deque in Python.

I know that it is implemented as a doubly link in Python. Does that mean that its time complexity is O(n)?


回答1:


deque are implemented a little smarter than just doubly-linked lists. They're a doubly-linked list of blocks of Python objects, where the left and right sides may be incomplete blocks.

The Big-O cost of accessing in the middle is still O(n), but it has a constant divisor (implementation dependent, CPython 3.5 allocates blocks that can store 64 objects). So if your deque has 1000 members, accessing in the middle still involves only around 7-8 "linked list-style" traversals, not 500-some. If the deque is smallish (65 to 128 elements, depending on how the empty slots align with the head and tail blocks), then lookup of any element is equal cost.



来源:https://stackoverflow.com/questions/39522787/time-complexity-of-random-access-in-deque-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!