list.sort(func) speed

mackstann mack at
Sun Aug 31 22:33:54 CEST 2003

On Sun, Aug 31, 2003 at 01:15:11PM -0700, Chad Netzer wrote:
> There is nothing about using a class that will make your program
> inherently slow, as long as you use the right algorithms.
> A data set of 20,000 is *small* by todays standards.  Sorting it should
> be trivial and near instantaneous using decorate-sort-undecorate.  Once
> it is sorted, you just keep the list sorted by doing insertions in
> sorted order (a very fast operation).  Is there a reason you need to do
> lots of re-sorts?

No, but the thought of inserting things in order did not quite occur to
me.  Can you give me an simple example of exactly how to go about this?
(all I can think of is a while loop with cmp() which seems too "manual")

> Perhaps you can use a dict to hold your data.  You can also extract all
> the keys you need to sort on, ahead of time, and keep it paired up with
> you data class or dictionary.  This will make decorate-sort-undecorate
> easy to code, and very quick to execute.  And you still have a flexible,
> easy to use data type.
> I'd strongly suggest you not avoid dicts or classes, just to try to
> speed up your code.  Almost all the time, they do not impair
> performance.  And if you complicate your design by avoiding them, you
> are putting yourself through unneeded pain.

It's actually the exact opposite;  at present I have no need (really!)
for metadata on the files, but I may in the future, at which point
using a dict or class for each song will probably be the way to go.

Although I 'spose I was assuming that using more complex objects for the
items in the list would result in a bigger speed hit than they really
would.  I just like to be cautious about things, you never know when you
might want to run your code on a really slow machine, and then, all of a
sudden, it's really slow. :)  Python people love to preach against
premature optimization, which I mostly agree with, but I also think it
is good to be conscious of possible speed issues (especially when the
size of the data set, and hardware speed are both quite variable, and
could both be more demanding than the ones I am testing with).

m a c k s t a n n  mack @
Money is the root of all evil, and man needs roots.

More information about the Python-list mailing list