Added profiler.

Merged
#13 · Created  · Last updated

Merged pull request

Merged in profiler (pull request #13)

f91747e·Author: ·Closed by: ·2019-03-16

Description

Note: This change depends on the logger. So the pull request #12 needs to be reviewed first before this one.

Purpose

Time profiling is important for us. Currently for profiling, we add timing codes manually for parts that are interesting, and we need to remove them after we’re done with them. Or we could use a profiler, but they are not always available on every platform, and would often give way too detailed results that might be hard to read. This profiler added is not to replace the dedicated profiling tools (like gprof), but to provide a simple and light weighted way to get the performance of certain parts of the code that we are interested in, so that one does not have to deal with getting system times every time a timer is needed.

Usage

To use the profiler, include util/profiler.h file.

All the profiler related codes will not have any effect unless USE_PROFILER macro is defined. This gives us a way to simply disable all the profiler related codes without removing them.

The basic timing class to use is profiler::Timer. The usage looks like this:

profiler::Timer tm("<Description>"); tm.start(); // Mark the current time as the starting point. // ... codes to be timed tm.elapse(); // Calculate the time elapsed from starting point. tm.report(); // Print the result (using LOG(INFO))

More easily, if the block to be timed is the surrounding scope, then a profiler::ScopeTimer can be used to measure the time elapsed between the construction and destruction.

{ profiler::ScopeTimer tm("<Description>"); // ... codes to be timed } // The time elapsed will be automatically measured and reported.

Sometimes, it is useful to collectively measure the time elapsed. For example, we are interested in the performance of a loop, where we only want to measure the total time inside a certain part in the loop. In this case, we could use profiler::TimerWorker or profiler::ScopeTimerWorker to report to profiler::TimerManager. For example,

profiler::TimerManager manager("<Description>"); while(some_condition) { { // ... codes that we are not interested in. } { profiler::ScopeTimerWorker tm(manager); // ... codes that we want to measure the time } // The worker will report the time elapsed to the manager. } manager.report(); // print the total time, as well as occurances of the worker.

0 attachments

0 comments

Loading commits...