list.sort(func) speed
Terry Reedy
tjreedy at udel.edu
Sat Aug 30 20:27:19 EDT 2003
"mackstann" <mack at incise.org> wrote in message
news:mailman.1062286079.6474.python-list at python.org...
> I have the following function that compares two filenames based on
their
> basename only:
>
> _basenameCmp = lambda self, *pair: cmp(*map(os.path.basename, pair))
> This is so that I can sort a list of files, and the sorting will be
done
> by filename, ignoring path. There is a minor issue which is not so
much
> a problem (yet) per se, but just made me curious if there was a
better
> way to go about this. When calling .sort() on my list, it takes
16ms to
> complete. Calling .sort(_basenameCmp) on the same list takes 244ms.
> This list could very well be 5 or 10 times the size, in use. Now, I
> believe that my _basenameCmp() needs to be called a lot of times
(er..
> can't figure out exactly how many, (len(mylist)/2)^2 ?) and doing a
> string operation that many times is obviously going to slow it down.
>
> So my question is: Is there anything I could do differently or
better
> to speed this up?
Google list for decorate sort undecorate pattern
TJR
More information about the Python-list
mailing list