On 9/14/11 2:41 PM, Benjamin Root wrote:
Are you sure the f2 code works? a.resize() takes only a shape tuple. As coded, you should get an exception.
wow, what an idiot!
I think I just timed how long it takes to raise that exception...
And when I fix that, I get a memory error.
When I fix that, I find that f3() wasn't doing the right thing. What an astonishing lack of attention on my part!
Here it is again, working, I hope!
In : %timeit f1() 10 loops, best of 3: 50.7 ms per loop
In : %timeit f2() 1000 loops, best of 3: 719 us per loop
In : %timeit f3() 100 loops, best of 3: 19 ms per loop
So: numpy.resize() is the slowest numpy.empty+ numpy.append() is faster numpy.ndarray.resize() is the fastest
Which matches my expectations, for once!
-Chris The code:
A test of various numpy re-sizing options
def f1(): """ numpy.resize """
extra = 100 l = extra a = numpy.zeros((l,)) for i in xrange(100): l += extra a = numpy.resize(a, (l,) )
def f2(): """ numpy.ndarray.resize """
extra = 100 l = extra a = numpy.zeros((l,)) for i in xrange(100): l += extra a.resize( (l,) )
def f3(): """ numpy.empty + append """
extra = 100 l = extra a = numpy.zeros((l,)) for i in xrange(100): b = numpy.empty((extra,)) a = numpy.append(a, b) return a
a1 = f1() a2 = f2() a3 = f3()
if a1.shape == a2.shape == a3.shape: print "they are all returning the same size array" else: print "Something is wrong!"