各种 `append` 效率比较

清单

  • list
    • append
    • 列表推导式
  • numpy
    • np.append
    • np.concatenate
    • np.vstack

注:代码中常用np代指numpy

测试环境

操作系统 CPU 编译环境
Window 10 Intel® Core™ i7-6500U CPU @ 2.5GHz Python 3.6.6

测试工具

timeit

效率比较

  • append
In [3]: %%timeit
   ...: a = []
   ...: for _ in range(10000):
   ...:     a.append([1, 2, 3])
   ...: a = np.asarray(a)
   ...:
4.26 ms ± 56 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  • 列表推导式
In [20]: %%timeit
    ...: a = [[1, 2, 3] for _ in range(10000)]
    ...: a = np.asarray(a)
    ...:
3.77 ms ± 39 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

少数情况下可用

  • np.append
In [7]: %%timeit
   ...: a = np.empty((0,3), int)
   ...: for _ in range(10000):
   ...:     a = np.append(a, [[1, 2, 3]], axis=0)
   ...:
55.5 ms ± 417 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
  • np.concatenate
In [15]: %%timeit
    ...: a = np.empty((0, 3), int)
    ...: for _ in range(10000):
    ...:     a = np.concatenate((a, [[1, 2, 3]]))
    ...:
45.1 ms ± 583 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
  • np.vstack
In [11]: %%timeit
    ...: a = np.empty((0, 3), int)
    ...: for _ in range(10000):
    ...:     a = np.vstack((a, [[1, 2, 3]]))
    ...:
    ...:
79.2 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

结论

numpy

xxx原因 得维护内容不变,所以每次 append 都得复制一遍 ndarray 内容

list

所以,直接用list添加好了

有关列表推导式

列表推导式数据量且过程复杂合并方面很多情况下不适用,且编程逻辑难以理解