python - Why is numpy.array so slow? -
i baffled this
def main(): in xrange(2560000): = [0.0, 0.0, 0.0] main() $ time python test.py real 0m0.793s let's see numpy:
import numpy def main(): in xrange(2560000): = numpy.array([0.0, 0.0, 0.0]) main() $ time python test.py real 0m39.338s holy cpu cycles batman!
using numpy.zeros(3) improves, still not enough imho
$ time python test.py real 0m5.610s user 0m5.449s sys 0m0.070s numpy.version.version = '1.5.1'
if wondering if list creation skipped optimization in first example, 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 optimised large amounts of data. give tiny 3 length array and, unsurprisingly, performs poorly.
consider 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 allocates memory. initial contents of array # random noise print 'python list:', pythontest.timeit(reps), 'seconds' print 'numpy array:', numpytest.timeit(reps), 'seconds' print 'uninitialised array:', uninitialised.timeit(reps), 'seconds' and output is
python list: 1.22042918205 seconds numpy array: 1.05412316322 seconds uninitialised array: 0.0016028881073 seconds it seem zeroing of array taking time numpy. unless need array initialised try using empty.
Comments
Post a Comment