[Numpy-discussion] Simple problem. Is it possible without a loop?
Vicente Sole
sole@esrf...
Wed Jun 9 10:31:44 CDT 2010
It gets even worse with data similar to those I will be using.
With:
x0 = numpy.linspace(-1,1, 10000)
niter = 2000
I get 24 seconds for option1 and 0.64 seconds for option2.
Considering I expect between 5 and 50 times that number of iterations,
the difference is already quite considerable.
Armando
Quoting Vicente Sole <sole@esrf.fr>:
>>> ? Well a loop or list comparison seems like a good choice to me. It is
>>> much more obvious at the expense of two LOCs. Did you profile the two
>>> possibilities and are they actually performance-critical?
>>>
>>> cheers
>>>
>
>
> The second is between 8 and ten times faster on my machine.
>
> import numpy
> import time
> x0 = numpy.arange(10000.)
> niter = 2000 # I expect between 10000 and 100000
>
>
> def option1(x, delta=0.2):
> y = [x[0]]
> for value in x:
> if (value - y[-1]) > delta:
> y.append(value)
> return numpy.array(y)
>
> def option2(x, delta=0.2):
> y = numpy.cumsum((x[1:]-x[:-1])/delta).astype(numpy.int)
> i1 = numpy.nonzero(y[1:]> y[:-1])
> return numpy.take(x, i1)
>
>
> t0 = time.time()
> for i in range(niter):
> t = option1(x0)
> print "Elapsed = ", time.time() - t0
> t0 = time.time()
> for i in range(niter):
> t = option2(x0)
> print "Elapsed = ", time.time() - t0
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list