Performance of list(…).insert(…)

后端 未结 3 1623
刺人心
刺人心 2021-01-01 22:24

I thought about the following question about computer\'s architecture. Suppose I do in Python

from bisect import bisect
index = bisect(x, a)      # O(log n)          


        
3条回答
  •  天涯浪人
    2021-01-01 23:05

    Python is a language. Multiple implementations exist, and they may have different implementations for lists. So, without looking at the code of an actual implementation, you cannot know for sure how lists are implemented and how they behave under certain circumstances.

    My bet would be that the references to the objects in a list are stored in contiguous memory (certainly not as a linked list...). If that is indeed so, then insertion using x.insert will cause all elements behind the inserted element to be moved. This may be done efficiently by the hardware, but the complexity would still be O(n).

    For small lists the bisect operation may take more time than x.insert, even though the former is O(log n) while the latter is O(n). For long lists, however, I'd hazard a guess that x.insert is the bottleneck. In such cases you must consider using a different data structure.

提交回复
热议问题