Perfromance stats are vital to understand dynamic efficiency of your code and may prove helpful to find and fix bottlenecks. This post discusses how to plug in these stats in your program.
There are primarily two classes of stats:
- Time stamps
Stats are derived from events. You would be interested to know how many time an event happened, and how long did you spend to complete an operation. So, segragate your events into either “count” or “time”. Time could be computed for best, average and worst for an operation.
Next, figure out the fundamental events, e.g. “How many time printf was called” or “what is the average time my program spend in each call”? To compute latter, you need the frequency of “printf” and total time spent in all calls of printf. Such events are dependent on other events.
Design of perf stat framework need to have following phases:
- Collection: Just collect raw data, most frequent operation.
- Processing: Perform all calculation to compute derived events
Please keep in mind that collection phase of events should be very quick. Do not perform any calculation during collection. This helps reducing intrusiveness of the event collection statements. The code to collect events should be tiny and can be inlined or #define’ed.