Aussie AI
Profiling and Benchmarking
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Profiling and Benchmarking
Performance profiling is the measurement of time and space efficiency metrics about your C++ program. Profiling is regarded as the right and proper first step when attempting to tune your code. You should run a profiler on a semi-realistic test run of your program under simulated production conditions to generate the performance data. Then you can analyze the data using the profiler's reports to find which functions are chewing up most of the time, or even which specific statements are busiest inside a heavily used function.
Benchmarking is a slightly different concept, and refers to testing the efficiency of certain operations, such as low-level operators, to find a more efficient way to do an operation. For example, if you want to compare multiplication versus addition, you write a program to run these operations a few million times. When changing a program to increase efficiency, you shouldn't assume that a certain operation is clearly faster, but you should benchmark whether the changes have noticeably increased the operation's efficiency (or even decreased it!).
Both profiling and benchmarking require data about CPU and memory usage. Techniques for measuring program efficiency range from the stop-watch method to the use of sophisticated profiler software tools. If no profiler is adequate, the programmer can gain timing information by adding instrumentation statements to the program, although there are many pitfalls in attempting to determine the time taken by a sequence of statements.
The measurement of the memory usage and space-efficiency of a C++ program is a slightly more difficult problem. There are several types of memory: instruction code, static memory, read-only string literals, initialization data, global/static variables, the stack, and the heap. Measuring the memory usage of the stack and heap is somewhat difficult because of their dynamic nature. However, various tools exist to measure the different types of memory, and clever use of C++ programming constructs can also yield reasonable data.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |