How is Python's List Implemented?

后端 未结 9 876
野趣味
野趣味 2020-11-22 08:00

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn\'t good enough to look at the source code.

9条回答
  •  萌比男神i
    2020-11-22 08:25

    It's a dynamic array. Practical proof: Indexing takes (of course with extremely small differences (0.0013 µsecs!)) the same time regardless of index:

    ...>python -m timeit --setup="x = [None]*1000" "x[500]"
    10000000 loops, best of 3: 0.0579 usec per loop
    
    ...>python -m timeit --setup="x = [None]*1000" "x[0]"
    10000000 loops, best of 3: 0.0566 usec per loop
    

    I would be astounded if IronPython or Jython used linked lists - they would ruin the performance of many many widely-used libraries built on the assumption that lists are dynamic arrays.

提交回复
热议问题