list.sort(func) speed

Raymond Hettinger vze4rx4y at
Sun Aug 31 04:33:06 CEST 2003


> 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?

A Schwartzian transform will completely remove the function call

Raymond Hettinger

More information about the Python-list mailing list