Why is numpy.array so slow?

前端 未结 4 1333
南笙
南笙 2020-11-27 21:54

I am baffled by this

def main():
    for i in xrange(2560000):
        a = [0.0, 0.0, 0.0]

main()

$ time python test.py

real     0m0.793s
<
4条回答
  •  失恋的感觉
    2020-11-27 22:47

    Numpy is optimised for large amounts of data. Give it a tiny 3 length array and, unsurprisingly, it performs poorly.

    Consider a separate test

    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'
    

    And the output is

    python list: 1.22042918205 seconds
    numpy array: 1.05412316322 seconds
    uninitialised array: 0.0016028881073 seconds
    

    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.

提交回复
热议问题