"Avoid Premature Optimization" Does Not Mean "Write Dumb Code"
prog21.dadgum.com
prog21.dadgum.com
If the result is more effort or less readable, then an understanding of what n is going to be like is probably worthwhile. For the love of all that's holy, please do not optimize access to a collection of days of the week that is accessed infrequently.
While it's still "pre-optimization" it's a smarter use of time to spend it where it will most likely matter.
You have to do your basic coding with your performance requirements in mind, because it is all too easy to come up with code that can never meet your performance requirements, no matter how much you profile and optimize it.
Case in point: http://strangelyconsistent.org/blog/p1-best-way-to-multiply-... . My solution (http://strangelyconsistent.org/p6cc2010/p1-colomon/) was quite elegant, IMO, setting up things so I simply generated all the all the possible solutions, and then took the min with respect to the number of multiplications.
But it was also appallingly inefficient. Generating all the possible solutions is completely the wrong approach, and no amount of optimizations to my algorithm would ever be sufficiently efficient for non-trivial problems. The only possible approach is to start over from scratch: http://strangelyconsistent.org/p6cc2010/p1-moritz/
So if I had properly considering my performance requirements, I would have come up with a much better solution. (Or just failed: dynamic programming is not really in my programming toolbox. I need to get around to remedying that!) But at the same time, worrying about how to most efficiently represent my Ordering object would have been completely pointless.
That's what "avoid premature optimization" means, IMO. It's not an excuse ignore performance. It's about not sweating the small details until you're certain they're important.
For example, the memoized word-splitting code at http://norvig.com/ngrams/ compared to the recent discussion here on HN of doing it with dynamic programming (as an interview problem).
The tuple representation of colors has algebraic qualities absent from the simplistic lookup-table he proposes (in fact, he's inadvertently taking us back from full color to "indexed" palettes common in the 80s)
I prefer the tuples better; at least they can be logically extended to include alpha & depth information, and can be efficiently packed into a machine word.)