[Numpy-discussion] load from text files Pull Request Review

Christopher Barker Chris.Barker@noaa....
Wed Sep 14 16:25:13 CDT 2011


On 9/14/11 1:01 PM, Christopher Barker wrote:
> numpy.ndarray.resize is a different method, and I'm pretty sure it
> should be as fast or faster that np.empty + np.append.

My profile:

In [25]: %timeit f1 # numpy.resize()
10000000 loops, best of 3: 163 ns per loop

In [26]: %timeit f2 #numpy.ndarray.resize()
10000000 loops, best of 3: 136 ns per loop

In [27]: %timeit f3 # numpy.empty() + append()
10000000 loops, best of 3: 136 ns per loop


those last two couldn't b more identical!

(though this is an excercise in unrequired optimization!)


My test code:

#!/usr/bin/env python

"""
test_resize

A test of various numpy re-sizing options

"""

import numpy

def f1():
     """
     numpy.resize
     """

     l = 100
     a = numpy.zeros((l,))
     for i in xrange(1000):
         l += l
         a = numpy.resize(a, (l,) )

     return None

def f2():
     """
     numpy.ndarray.resize
     """

     l = 100
     a = numpy.zeros((l,))
     for i in xrange(1000):
         l += l
         a.resize(a, (l,) )

     return None

def f3():
     """
     numpy.empty + append
     """

     l = 100
     a = numpy.zeros((l,))
     for i in xrange(1000):
         b = np.empty((l,))
         a.append(b)

     return None





-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the NumPy-Discussion mailing list