3

Normally, if my code has any performance issue, I will follow the below steps to understand the cause.

  1. top command to see cpu's usage
  2. free command to see memory usage
  3. add timing information to the code
  4. print progress statements in the code
  5. understand and improve the algorithm

What do you do, if your code is running very slow.

Boolean
  • 13,126
  • 28
  • 82
  • 127

3 Answers3

6

1) with a profiler, spot the slowest part of your code
2) once you've found them, think of a way to improve them
step 2 is the most difficult. You could need some small changes or rewrite everything

BlackBear
  • 20,590
  • 9
  • 41
  • 75
1

Use a Profiler. The location of resource bottlenecks can be non-intuitive.

Blastfurnace
  • 17,441
  • 40
  • 50
  • 66
1

Well, yes, profile, but don't use just any old profiler.

You need to look at lines of code that are on the stack a good percent of the time, because that is how much you could potentially save by optimizing them.

To find them, you should use a profiler that

  • takes wall-clock-time stack samples (during I/O as well as CPU time),
  • when you want them (when it's being slow, not all the time),
  • and reports by line of code (not just by function/method)
  • the percent of samples containing that line (not count, not self time, not average time - percent).

A good one is Zoom.

Another very simple method is random-pausing.

Community
  • 1
  • 1
Mike Dunlavey
  • 38,662
  • 12
  • 86
  • 126
  • @BlackBear: It's amazing how much [entrenched confusion](http://stackoverflow.com/questions/4387895/if-profiler-is-not-the-answer-what-other-choices-do-we-have/4390868#4390868) there is on this subject. Also [this](http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343). – Mike Dunlavey Mar 01 '11 at 15:47
  • sorry, I mean for your answer about random-pausing. And I agree, the issue about profiler is find a good one. – BlackBear Mar 01 '11 at 15:50