Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
in thread 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".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://136103]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-07-25 19:38 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (377 votes). Check out past polls.