Timing system in NeL

Introduction

This page will describe all classes related to the time on NeL. All these classes are in the NeLMisc library.

CTime in time_nl.h

CTime contains only static methods to get the time, in second since Unix epoch, in millisecond or using processor ticks. Check time_nl.h or Doxygen for more information.

CCPUTimeStat in cpu_time_stat.h

Only works on Unices. Allows accurate timing measures for both cpu and process (at least at OS timing accuracy). Check cpu_time_stat.h or Doxygen for more information.

CStopWatch in stop_watch.h

Stopwatch class used for performance measurements and statistics. It allows you to measure the time elapsed with controls like ''start'', ''stop'', ''pause'', and ''resume''.
Check stop_watch.h or Doxygen for more information.

  • Simple Stop Watch usage is straight forward:
 1void foo()
 2{
 3    // create the stop watch.
 4    NLMISC::CStopWatch stopWatch;
 5    // start the timer.
 6    stopWatch.start();
 7    // ... process some code here.
 8    // stop the timer.
 9    stopWatch.stop();
10    // and output the results.
11    nlinfo("Time Elapsed in Milliseconds: %d", stopWatch.getDuration());
12}
  • Some slightly more advanced options are:
 1void foo()
 2{
 3    // create the stop watch.
 4    NLMISC::CStopWatch stopWatch;
 5
 6    // start the timer.
 7    stopWatch.start();
 8
 9    // ... process some code here.
10
11    // pause the timer..
12    stopWatch.pause();
13
14    // ... process some code that shouldn't affect your dynamically.
15    // add the time we want to assume the above code took.
16    stopWatch.addTime( /* time here is in ticks... */ 1500 );
17
18    // resume the timer.
19    stopWatch.resume();
20
21    // ... process some code here.
22
23    // stop the timer.
24    stopWatch.stop();
25
26    // start the timer.
27    stopWatch.start();
28    // ... process the same code here.
29    // stop the timer.
30    stopWatch.stop();
31
32    // and output the results.
33    nlinfo("Time Elapsed in Milliseconds: %d", stopWatch.getDuration());
34    nlinfo("Average Time of Both start/stop calls in Milliseconds: %d", stopWatch.getAverageDuration());
35}

CHTimer in hierarchical_timer.h

Allows to accurately measure performance of routines, and displays results hierarchically. It's the best way to know what is slow in your program. It's really easy to use and give you good measures.

  • Create a function to activate/deactivate the bench.
 1void switchBench()
 2{
 3    static bool benching = false;
 4    benching = !benching;
 5    if(benching)
 6    {
 7        nlinfo("Start of benchmark");
 8        CHTimer::startBench();
 9    }
10    else
11    {
12        nlinfo("End of benchmark");
13        CHTimer::endBench();
14        CHTimer::display(InfoLog, CHTimer::TotalTime, true, true);
15        CHTimer::displayHierarchicalByExecutionPathSorted(InfoLog, CHTimer::TotalTime, true, 64, 3);
16    }
17}
  • Then add H_BEFORE and H_AFTER around each code block you want to bench.
1H_BEFORE( nameofbenchinlist );
2// ... your code to bench ...
3H_AFTER( nameofbenchinlist );
  • If you use H_AUTO, it'll bench the current scope.
 1void func()
 2{
 3  H_AUTO( func ); // it'll bench the func() scope
 4  // ...some code...
 5  if(i<j)
 6  {
 7    H_AUTO( func_less ); // it'll bench the if() scope
 8    // ...some code...
 9  }
10  // ...some code...
11}