python - 哪个更快? `list.pop(0)` 与 `del list[0]` ?

标签 python list performance

哪个列表操作更快? list.pop(0)del list[0]

最佳答案

免责声明快速而肮脏的基准:

在 Python 3.7.6 上使用 IPython 时,del list[0] 似乎更快,因为它只需要大约 65% 的时间list.pop(0) 花费的时间。

使用的命令:

## Baseline to be subtracted
%timeit lst = list(range(10))
# >> 230 ns ± 1.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

## list.pop(0) time taken
%timeit lst = list(range(10)); lst.pop(0)
# >> 281 ns ± 0.926 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

## del list[0] time taken
%timeit lst = list(range(10)); del lst[0]
# >> 263 ns ± 1.11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

(263-230)/(281-230) = 33/51 = 65%

关于python - 哪个更快? `list.pop(0)` 与 `del list[0]` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60425973/

相关文章:

python - 计算平方和与平方和之差的函数

python - "RuntimeError: generator raised StopIteration"每次我尝试运行应用程序时

python - 无法从 React.js 数据网格中抓取数据

c++ - 多线程链表遍历

python - 列表索引 - 如果太大则返回到列表的开头

python - 使用 Monitor 录制 OpenAI 健身房视频

Python:将csv文件中的行数据放入列表中

javascript - HTML 选择字段渲染需要时间

SQL Group By with Count 很慢

Java:在大字典中搜索字符串的非常快速的方法