为什么numpy.array这么慢? [英] Why is numpy.array so slow?
问题描述
我对此感到困惑
def main():
for i in xrange(2560000):
a = [0.0, 0.0, 0.0]
main()
$ time python test.py
real 0m0.793s
现在让我们来看看numpy:
Let's now see with numpy:
import numpy
def main():
for i in xrange(2560000):
a = numpy.array([0.0, 0.0, 0.0])
main()
$ time python test.py
real 0m39.338s
神圣的CPU周期蝙蝠侠!
Holy CPU cycles batman!
使用numpy.zeros(3)
可以改善,但是恕我直言
Using numpy.zeros(3)
improves, but still not enough IMHO
$ time python test.py
real 0m5.610s
user 0m5.449s
sys 0m0.070s
numpy.version.version ='1.5.1'
numpy.version.version = '1.5.1'
如果您想知道在第一个示例中是否跳过了列表创建以进行优化,则不是:
If you are wondering if the list creation is skipped for optimization in the first example, it is not:
5 19 LOAD_CONST 2 (0.0)
22 LOAD_CONST 2 (0.0)
25 LOAD_CONST 2 (0.0)
28 BUILD_LIST 3
31 STORE_FAST 1 (a)
推荐答案
Numpy已针对大量数据进行了优化.给它一个很小的3长度数组,毫不奇怪,它的性能很差.
Numpy is optimised for large amounts of data. Give it a tiny 3 length array and, unsurprisingly, it performs poorly.
考虑单独的测试
import timeit
reps = 100
pythonTest = timeit.Timer('a = [0.] * 1000000')
numpyTest = timeit.Timer('a = numpy.zeros(1000000)', setup='import numpy')
uninitialised = timeit.Timer('a = numpy.empty(1000000)', setup='import numpy')
# empty simply allocates the memory. Thus the initial contents of the array
# is random noise
print 'python list:', pythonTest.timeit(reps), 'seconds'
print 'numpy array:', numpyTest.timeit(reps), 'seconds'
print 'uninitialised array:', uninitialised.timeit(reps), 'seconds'
输出为
python list: 1.22042918205 seconds
numpy array: 1.05412316322 seconds
uninitialised array: 0.0016028881073 seconds
似乎是数组的归零一直花费在numpy上.因此,除非您需要初始化数组,否则请尝试使用empty.
It would seem that it is the zeroing of the array that is taking all the time for numpy. So unless you need the array to be initialised then try using empty.
这篇关于为什么numpy.array这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!