How to Optimize Your Program's Performance
Start your program running under the debugger., While it is being slow, halt it manually., Record the call stack., Restart it, halt it again, and record again., Examine the call stack samples for common statements., Examine statements that appear on...
Step-by-Step Guide
-
Step 1: Start your program running under the debugger.
If it is a kind of program that normally runs very fast, but you want it to go faster, you can add a temporary outer loop of say, 1000 times, to give you enough time to halt it. -
Step 2: While it is being slow
Do this by striking a break or pause key that makes the program stop wherever it happens to be.
Do not do this by setting a breakpoint in the code, because then you will not be able to see what it was doing when you struck the key. , What you need to record is the file and line number of each statement on the call stack.
Hopefully there is an easy way to copy-paste this information into a text editor, but if not, do it the hard way, with paper and pencil. , You can either continue execution from where you were, or completely restart the program.
Either way is OK.
You can do this several times, up to about
20. , You may find it helpful to group the call stack samples into an indented tree structure, so you can see the commonality between samples.
It is important to remember that you are capturing statements, not functions.
The line numbers are really important. , These are your candidates for optimization.
For example, if there is some statement at file Foo.cpp, line 127, that happens to show up on 8 out of 20 samples, then you should take a good look at that statement and see if what it is doing is really necessary, because if you could get rid of it, you would save about 40% of the execution time. , And enjoy the speedup. , Until you can't find anything else to optimize.
Chances are your program is now many times faster than it was to begin with. -
Step 3: halt it manually.
-
Step 4: Record the call stack.
-
Step 5: Restart it
-
Step 6: halt it again
-
Step 7: and record again.
-
Step 8: Examine the call stack samples for common statements.
-
Step 9: Examine statements that appear on multiple call stacks.
-
Step 10: Fix the problem.
-
Step 11: Do it all again
-
Step 12: and again.
Detailed Guide
If it is a kind of program that normally runs very fast, but you want it to go faster, you can add a temporary outer loop of say, 1000 times, to give you enough time to halt it.
Do this by striking a break or pause key that makes the program stop wherever it happens to be.
Do not do this by setting a breakpoint in the code, because then you will not be able to see what it was doing when you struck the key. , What you need to record is the file and line number of each statement on the call stack.
Hopefully there is an easy way to copy-paste this information into a text editor, but if not, do it the hard way, with paper and pencil. , You can either continue execution from where you were, or completely restart the program.
Either way is OK.
You can do this several times, up to about
20. , You may find it helpful to group the call stack samples into an indented tree structure, so you can see the commonality between samples.
It is important to remember that you are capturing statements, not functions.
The line numbers are really important. , These are your candidates for optimization.
For example, if there is some statement at file Foo.cpp, line 127, that happens to show up on 8 out of 20 samples, then you should take a good look at that statement and see if what it is doing is really necessary, because if you could get rid of it, you would save about 40% of the execution time. , And enjoy the speedup. , Until you can't find anything else to optimize.
Chances are your program is now many times faster than it was to begin with.
About the Author
Thomas Gray
A passionate writer with expertise in creative arts topics. Loves sharing practical knowledge.
Rate This Guide
How helpful was this guide? Click to rate: