在Python中,对于多个应用程序,我通常必须将值存储到数组中,例如:
results = []
for i in range(num_simulations):
...<calculate results_new>...
results.append(results_new)
然而,我已经看到大多数其他示例代码首先声明一个零值数组:
results = np.zeros(len(num_simulations))
for i in range(num_simulations):
...<calculate results_new>...
results[i] = results_new
哪一种更适合常规练习?或者即使您必须进行性能比较,这两种方法之间的时间和内存是否真的存在显着差异?
免责声明:我或多或少只使用Python代码进行模拟,因此我只想在实践中实现更好的实践。
答案 0 :(得分:0)
关于使用numpy数组,您应该了解一些事项:
因此,在您的应用程序中,如果您只是将结果存储在列表中,而不是执行任何数值方法,那么如果您不使用numpy就可以了。事实上,这样做效率更高,如下所示
In [29]: %%timeit
...: results=[]
...: num_simulations=10000
...: for i in range(num_simulations):
...: results.append(i)
...:
1000 loops, best of 3: 984 µs per loop
In [30]: %%timeit
...: num_simulations = 10000
...: results=np.zeros(num_simulations)
...: for i in range(num_simulations):
...: results[i]=i
...:
1000 loops, best of 3: 1.44 ms per loop
In [31]: %%timeit
...: results=[]
...: num_simulations=100000
...: for i in range(num_simulations):
...: results.append(i)
...:
100 loops, best of 3: 10.1 ms per loop
In [32]: %%timeit
...: num_simulations = 100000
...: results=np.zeros(num_simulations)
...: for i in range(num_simulations):
...: results[i]=i
...:
100 loops, best of 3: 15.4 ms per loop
In [33]: %%timeit
...: results=[]
...: num_simulations=1000000
...: for i in range(num_simulations):
...: results.append(i)
...:
10 loops, best of 3: 103 ms per loop
In [34]: %%timeit
...: num_simulations = 1000000
...: results=np.zeros(num_simulations)
...: for i in range(num_simulations):
...: results[i]=i
...:
10 loops, best of 3: 156 ms per loop
总结一下结果,
Normal list vs Numpy
984 vs 1440 microsecond for 10000 simulations
10.1 vs 15.4 millisecond for 100000 simulations
103 vs 156 millisecond for 1000000 simulations
显然,仅使用列表,纯粹用于存储更快,因为它不涉及堆内存分配开销
然而,对于你想要在矩阵上执行的几乎任何数值方法,Numpy提供了更多的黯然失色的好处