list.sort(func) speed
mackstann
mack at incise.org
Sat Aug 30 21:58:42 EDT 2003
On Sat, Aug 30, 2003 at 08:27:19PM -0400, Terry Reedy wrote:
> Google list for decorate sort undecorate pattern
Awesome, thanks!
I tried it three ways (including original), and timed each one a few
times.
-------------------
# 1. original way
origlist.sort(self._basenameCmp)
# 2. decorate sort undecorate w/ list comp.
newlist = [ (os.path.basename(i), i) for i in origlist ]
newlist.sort()
origlist = [ i[1] for i in newlist ]
# 3. decorate sort decorate w/ zip and map
newlist = zip(map(os.path.basename, origlist), xrange(len(origlist)))
newlist.sort()
origlist = [ origlist[index] for basename,index in newlist ]
-------------------
And here are the times:
1. Around 300ms
2. 75-80ms
3. 70-75ms
And after further tweaking, I arrived at what seems to be the simplest
and fastest way to go about it (variation on #3):
newlist = [ pair[::-1] for pair in
enumerate(map(os.path.basename, oldlist)) ]
newlist.sort()
origlist = [ origlist[index] for basename,index in newlist ]
Thanks,
--
m a c k s t a n n mack @ incise.org http://incise.org
What this world needs is a good five-dollar plasma weapon.
More information about the Python-list
mailing list