[Tutor] Simplicity vs. Speed
Roeland Rengelink
r.b.rigilink@chello.nl
Tue, 15 May 2001 01:52:45 +0200
Hi Phil,
> Phil Bertram wrote:
>
> Hi all,
>
> I have a program that tracks scores and the results from a sports
> competition. Points are given for people who pick the winning team.
>
> The data is held classes that have attributes that are lists of lists
> of lists etc.
>
> I did it this way because I imagined that it would be faster as I
> would only have to iterate through 10 items rather than 100
>
> eg.
>
> Week1 = ['Game1', ........ 'Game10']
> Week2 = ['Game11',.........'Game20] etc etc
>
> The above example is very simplified, data levels go 3 or 4 deep and
> so things sometimes become a little unclear.
>
> I'm now thinking that it is better to keep data in just a single list
> and filter the list each time I need to do an operation.
>
> eg
>
> Games = ['Game1',..................'Game100']:
> for game in games:
> if game.week == 'Week1':
> do something
>
> Or would using the 'filter' function be more or less efficient ?
>
I really don't know. I can guess, of course, but my guess is less
reliable than your measurement.
>
> Would you suggest that a new programmer just keep things simple so as
> to get the code working bug free, and not worry at all about speed ?
>
Yes!
Especially since simplest is usually also fastest.
First get it working.
Does it work fast enough?
(fast enough is totally subjective and the only relevant criterium)
if yes:
leave well enough alone
if no:
profile!
(measurement is the only objective criterium)
optimize the parts you _know_ are slow.
- optimize by redisigning your algorithms
- keep measuring
if your best algorithm is still too slow
- rewrite in C
In my experience slow code falls in one of three categories.
1. slow, but not slow enough to be worth the trouble
- I don't care if something runs in 0.1 second rather than 1 second
2. so slow that it's not worth the trouble.
- I don't care if something runs in 1 hour rather than 10 hours
3. Hmm, maybe
- I may care if something runs in 1 second rather than 10 seconds
3 is, of course, the rarest category.
Having made the obligatory point about only rewriting for optimization
iff:
1. you are sure it is necessary, and
2. you have reaon to believe it may help
let me add that a nagging feeling that another approach would be more
elegant/shorter/neater or simply feel better is a perfectly good reason
to rewrite your code. The nice thing is that if your feeling happens to
be right, your code will usually become faster too.
Hope this helps,
--
r.b.rigilink@chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"