[英]Why numpy array takes more time than list?
Why is numpy slower in this code?为什么 numpy 在此代码中较慢?
for i in range(10000):
array = [[0.0,] * 1024 for x in range(1024)]
0,021539204 seconds time elapsed (39.616.810 instructions)经过 0,021539204 秒的时间(39.616.810 指令)
import numpy as np
for i in range(10000):
array = np.zeros((1024,1024))
0.209111860 seconds time elapsed (1.067.923.180 instructions)经过 0.209111860 秒的时间(1.067.923.180 指令)
Are you running in the exact same machine?您是否在完全相同的机器上运行? I'm getting faster result in numpy
.我在numpy
的结果越来越快。
In [7]: %%time
...: import numpy as np
...: for i in range(10000):
...: array = np.zeros((1024,1024))
...:
CPU times: user 3.33 s, sys: 0 ns, total: 3.33 s
Wall time: 3.32 s
In [8]: %%time
...: for i in range(10000):
...: array = [[0.0,] * 1024 for x in range(1024)]
...:
CPU times: user 1min 14s, sys: 0 ns, total: 1min 14s
Wall time: 1min 14s
This answer in the numpy
vs list
thread also agrees. numpy
vs list
线程中的这个答案也同意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.