On Wed, Sep 14, 2011 at 5:30 PM, Christopher Barker <[email protected]> wrote: > 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 [107]: %timeit f1() > 10 loops, best of 3: 50.7 ms per loop > > In [108]: %timeit f2() > 1000 loops, best of 3: 719 us per loop > > In [109]: %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! >
Good catch! I didn't think the difference between np.resize and ndarray.resize would matter. (And I was getting inscrutable errors when I called ndarray.resize that told me to use np.resize instead.) -Chris JS > -Chris > The code: > > #!/usr/bin/env python > > """ > test_resize > > A test of various numpy re-sizing options > > """ > > import numpy > > def f1(): > """ > numpy.resize > """ > > extra = 100 > l = extra > a = numpy.zeros((l,)) > for i in xrange(100): > l += extra > a = numpy.resize(a, (l,) ) > > return a > > def f2(): > """ > numpy.ndarray.resize > """ > > extra = 100 > l = extra > a = numpy.zeros((l,)) > for i in xrange(100): > l += extra > a.resize( (l,) ) > > return a > > 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!" > > > -- > 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 > > [email protected] > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
