Where can I find the time and space complexity of the built-in sequence types in Python

a 夏天 提交于 2019-12-12 07:42:05

问题


I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?


回答1:


Checkout the TimeComplexity page on the py dot org wiki. It covers set/dicts/lists/etc at least as far as time complexity goes.




回答2:


Raymond D. Hettinger does an excellent talk (slides) about Python's built-in collections called 'Core Python Containers - Under the Hood'. The version I saw focussed mainly on set and dict, but list was covered too.

There are also some photos of the pertinent slides from EuroPython in a blog.

Here is a summary of my notes on list:

  • Stores items as an array of pointers. Subscript costs O(1) time. Append costs amortized O(1) time. Insert costs O(n) time.
  • Tries to avoid memcpy when growing by over-allocating. Many small lists will waste a lot of space, but large lists never waste more than about 12.5% to overallocation.
  • Some operations pre-size. Examples given were range(n), map(), list(), [None] * n, and slicing.
  • When shrinking, the array is realloced only when it is wasting 50% of space. pop is cheap.



回答3:


If your asking what I think your asking, you can find them Here... page 476 and on.

It's written around optimization techniques for Python; It's mostly Big-O notation of time efficiencies not much memory.



来源:https://stackoverflow.com/questions/45228/where-can-i-find-the-time-and-space-complexity-of-the-built-in-sequence-types-in

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