[Numpy-discussion] Timing array construction

Mark Janikas mjanikas@esri....
Wed Apr 29 20:15:54 CDT 2009


Hello All,

I was exploring some different ways to concatenate arrays, and using "c_" is the fastest by far.  Is there a difference I am missing that can account for the huge disparity?  Obviously the "zip" function makes the "as array" and "array" calls slower, but the same arguments (xCoords, yCoords) are being passed to the methods... so if there is no difference in the outputs (there doesn't appear to be) then what reason would I have to use "array" or "as array" in this context?  Thanks so much ahead of time..

MJ

############## Snippet ###################
import numpy as NUM

def useAsArray(xCoords, yCoords):
    return NUM.asarray(zip(xCoords, yCoords))

def useArray(xCoords, yCoords):
    return NUM.array(zip(xCoords, yCoords))

def useC(xCoords, yCoords):
    return NUM.c_[xCoords, yCoords]


if __name__ == "__main__":
    from timeit import Timer
    import numpy.random as RAND
    import collections as COLL

    resAsArray = COLL.defaultdict(float)
    resArray = COLL.defaultdict(float)
    resMat = COLL.defaultdict(float)
    numTests = 0.0
    sameTests = 0.0
    N = [100, 200, 400, 800, 1600, 3200, 6400, 12800]
    for i in N:
        print "Time Join List into Array for N = " + str(i)
        xCoords = RAND.normal(10, 1, i)
        yCoords = RAND.normal(10, 1, i)

        statement = 'from __main__ import xCoords, yCoords, useAsArray'
        t1 = Timer('useAsArray(xCoords, yCoords)', statement)
        resAsArray[i] = t1.timeit(10)

        statement = 'from __main__ import xCoords, yCoords, useArray'
        t2 = Timer('useArray(xCoords, yCoords)', statement)
        resArray[i] = t2.timeit(10)

        statement = 'from __main__ import xCoords, yCoords, useC'
        t3 = Timer('useC(xCoords, yCoords)', statement)
        resMat[i] = t3.timeit(10)

    for n in N:
        print "%i, %0.4f, %0.4f, %0.4f" % (n, resAsArray[n], resArray[n], resMat[n])
###############################################################

RESULT

N, useAsArray, useArray, useC
100, 0.0066, 0.0065, 0.0007
200, 0.0137, 0.0140, 0.0008
400, 0.0277, 0.0288, 0.0007
800, 0.0579, 0.0577, 0.0008
1600, 0.1175, 0.1289, 0.0009
3200, 0.2291, 0.2309, 0.0012
6400, 0.4561, 0.4564, 0.0013
12800, 0.9218, 0.9122, 0.0019


Mark Janikas
Product Engineer
ESRI, Geoprocessing
380 New York St.
Redlands, CA 92373
909-793-2853 (2563)
mjanikas@esri.com<mailto:mjanikas@esri.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090429/279248f1/attachment-0001.html 


More information about the Numpy-discussion mailing list