Perl Monk, Perl Meditation PerlMonks

### Re(2): Premature optimization

by FoxtrotUniform (Prior)
 on Jan 04, 2002 at 01:38 UTC ( #136103=note: print w/replies, xml ) Need Help??

in reply to (tye)Re: Premature optimization

My usual procedure for optimizing something is:

1. Profile it, to find out which part of the problem is taking the most time. (Sometimes the problem's a single algorithm, in which case this isn't really necessary.)
2. Estimate the asymptotic bound of that bit of the problem. (Yet another excellent real-world use for those icky awful theory courses you were forced to take in university. :-)
3. Try to make the algorithm faster. Sometimes this just isn't possible; IIRC, you can't get a general sort on a single-processor machine faster than O(n log n). But you can often simplify the problem: put a few restrictions on what you sort, and you can get down to O(n). Nobody knows how to solve the travelling salesman problem in less than exponential time, but if you don't need a perfect solution, just one no worse than twice as bad as optimal, you can do it in polynomial (cubic, IIRC) time.
4. Repeat until you run out of relevant sub-problems.
5. If it still isn't fast enough, profile it again; this time, look for lines and functions that get executed most often, and try to slim those down. If your program's data set is particularly small, this might be more important than step 2; then again, if n is small, your program's probably "fast enough".
--
:wq

Create A New User
Node Status?
node history
Node Type: note [id://136103]
help
Chatterbox?
 [LanX]: as long as he has votes left, the nodelet remains [LanX]: There is a very simple solution ... [marinersk]: Correct, so one workaround is to leave one vote. [marinersk]: But I was looking for a more elegant solution. It appears noone online at this time is aware of one. [LanX]: go to Nodelet Settings and click "All nodelets off" [LanX]: SUPER STABLE!!! [marinersk]: LanX++ LOL Yes, that is another workaround. :-) [LanX]: strangely you can't disable the XP nodelet [LanX]: But you can use CSS to hide it [LanX]: and disoplay the data permanently in your peronal nodelet

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-05-29 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.