[Numpy-discussion] Initialization of array?

Rob W. W. Hooft rob at hooft.net
Thu Nov 30 09:33:46 CST 2000


 KH> I had another look at the definition of "ones" and of another routine
 KH> I frequently use: arange. It appears that even without rewriting them
 KH> in C, some speedup can be achieved:
 KH> 
 KH> - in ones(), the + 1 should be done "in place", saving about 15%, more
 KH> if you run out of processor cache:

 KH> I'd also try assignment in place:

 KH> def ones(shape, typecode='l', savespace=0):
 KH>    a = zeros(shape, typecode, savespace)
 KH>    a[len(shape)*[slice(0, None)]] = 1
 KH>    return a

This is even faster, but it is better to write "a[...] = 1", because
your manual calculation of "..." gives a large overhead for small arrays.

On another machine this time:
    Numeric.ones             10 ->    0.254ms
    Numeric.ones            100 ->    0.268ms
    Numeric.ones           1000 ->    0.340ms
    Numeric.ones          10000 ->    1.960ms
    Numeric.ones         100000 ->   29.300ms
    Numeric.zeros            10 ->    0.055ms
    Numeric.zeros           100 ->    0.059ms
    Numeric.zeros          1000 ->    0.068ms
    Numeric.zeros         10000 ->    0.430ms
    Numeric.zeros        100000 ->    9.800ms
    Add inplace              10 ->    0.246ms
    Add inplace             100 ->    0.255ms
    Add inplace            1000 ->    0.312ms
    Add inplace           10000 ->    1.270ms
    Add inplace          100000 ->   18.100ms
    Assign inplace           10 ->    0.192ms
    Assign inplace          100 ->    0.201ms
    Assign inplace         1000 ->    0.242ms
    Assign inplace        10000 ->    1.010ms
    Assign inplace       100000 ->   16.300ms
    Reshape 1                10 ->    0.842ms
    Reshape 1               100 ->    1.175ms
    Reshape 1              1000 ->    4.100ms
    Reshape 1             10000 ->   35.100ms
    Reshape 1            100000 ->  368.600ms

Rob

-- 
=====   rob at hooft.net          http://www.hooft.net/people/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_ones.py
Type: application/octet-stream
Size: 957 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20001130/b2dcebec/attachment-0001.obj 


More information about the Numpy-discussion mailing list