Timer is important for measuring speed-ups, or elapsed time for comparing different algorithms.

We used to import <ctime> and measure the elapsed time with the following code:

However, the clock() function measures the CPU time, not actual time elapsed (which may be much greater), according to this StackOverFlow thread.

Hence, here I share my code snippets to measure the exact execution time in C++ as a DebugTimer Class.

It also supports using an average window to measure the average time within AVERAGE_WINDOW_SIZE.

It’s a static class, so it’s very easy to use, and it supports multiple threads:

That’s it!


Another way to meaasure time is to use:

I personally don’t like this simple expression, but it also works well.