LISTS: Extract every other element - SUMMARY
Mike Fletcher
mfletch at tpresence.com
Sun Dec 19 14:40:21 EST 1999
Not with my testing on the code-as-given (incidentally, code as given had a
nameerror on p in the second loop setup). I get:
1.098 -- algo7
1.052 -- for with multiply
1.134 -- for with divide
0.421 -- matrix step-slice
0.428 -- matrix reshape
Those are with lists of size 10**7, similar results are seen at each size.
However, your algo can be optimised to be twice as fast as in the following:
def algo8(data):
'''Chris' hard coded data slice without function call'''
l = len(data)
res = (l+1)/2 * [None]
p=0
for p in xrange(0, l-99, 100):
q = p/2
d = data[p:p+100]
res[q:q+50] = [
d[ 1],d[ 3],d[ 5],d[ 7],d[ 9],d[11],d[13],d[15],d[17],d[19],
d[21],d[23],d[25],d[27],d[29],d[31],d[33],d[35],d[37],d[39],
d[41],d[43],d[45],d[47],d[49],d[51],d[53],d[55],d[57],d[59],
d[61],d[63],d[65],d[67],d[69],d[71],d[73],d[75],d[77],d[79],
d[81],d[83],d[85],d[87],d[89],d[91],d[93],d[95],d[97],d[99],
]
for i in range(p+1, l, 2): res[i/2] = data[i]
return res
Still a tad shy of the Numeric results. But a big improvement over the
other non-Numeric approaches. Time for this in testing on the same 10**7
list is 0.540 .
Enjoy all,
Mike
-----Original Message-----
From: Christian Tismer [mailto:tismer at appliedbiometrics.com]
Sent: Sunday, December 19, 1999 1:16 PM
To: Randall Hopper
Cc: Adrian Eyre; python-list at python.org; Mike Fletcher
Subject: Re: LISTS: Extract every other element - SUMMARY
Hi Kids,
the following will outperform everything by far,
also Numeric. hee hee.
...
More information about the Python-list
mailing list