How is Python#39;s List Implemented?(Python 的 List 是如何实现的?)
问题描述
是链表还是数组?我四处寻找,只发现人们在猜测.我的 C 知识还不够好,无法查看源代码.
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.
推荐答案
这是一个动态数组.实际证明:无论索引如何,索引都需要(当然差异非常小(0.0013 µsecs!))相同的时间:
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
如果 IronPython 或 Jython 使用链表,我会感到震惊 - 它们会破坏许多基于列表是动态数组的假设而广泛使用的库的性能.
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.
这篇关于Python 的 List 是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 的 List 是如何实现的?
基础教程推荐
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
