list.sort(func) speed

mackstann mack at incise.org
Sat Aug 30 19:25:32 EDT 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?

TIA,
-- 
m a c k s t a n n  mack @ incise.org  http://incise.org
Pascal is not a high-level language.
		-- Steven Feiner





More information about the Python-list mailing list