NelTime

Version 1 (kervala, 09/29/2010 08:46 pm)

1 1 kervala
h1. Timing system in NeL
2 1 kervala
3 1 kervala
h2. Introduction
4 1 kervala
5 1 kervala
This page will describe all classes related to the time on NeL. All these classes are in the NeLMisc library.
6 1 kervala
7 1 kervala
h2. CTime in time_nl.h
8 1 kervala
9 1 kervala
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":http://dev.ryzom.com/ext/doxygen/nel/index.html for more information.
10 1 kervala
11 1 kervala
h2. CCPUTimeStat in cpu_time_stat.h
12 1 kervala
13 1 kervala
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":http://dev.ryzom.com/ext/doxygen/nel/index.html for more information.
14 1 kervala
15 1 kervala
h2. CStopWatch in stop_watch.h
16 1 kervala
17 1 kervala
Stopwatch class used for performance measurements and statistics. It allows you to measure the time elapsed with controls like ''start'', ''stop'', ''pause'', and ''resume''.
18 1 kervala
Check stop_watch.h or "Doxygen":http://dev.ryzom.com/ext/doxygen/nel/index.html for more information.
19 1 kervala
20 1 kervala
* Simple Stop Watch usage is straight forward:
21 1 kervala
22 1 kervala
<pre><code class="CPP">void foo()
23 1 kervala
{
24 1 kervala
    // create the stop watch.
25 1 kervala
    NLMISC::CStopWatch stopWatch;
26 1 kervala
    // start the timer.
27 1 kervala
    stopWatch.start();
28 1 kervala
    // ... process some code here.
29 1 kervala
    // stop the timer.
30 1 kervala
    stopWatch.stop();
31 1 kervala
    // and output the results.
32 1 kervala
    nlinfo("Time Elapsed in Milliseconds: %d", stopWatch.getDuration());
33 1 kervala
}</code></pre>
34 1 kervala
35 1 kervala
* Some slightly more advanced options are:
36 1 kervala
37 1 kervala
<pre><code class="CPP">void foo()
38 1 kervala
{
39 1 kervala
    // create the stop watch.
40 1 kervala
    NLMISC::CStopWatch stopWatch;
41 1 kervala
42 1 kervala
    // start the timer.
43 1 kervala
    stopWatch.start();
44 1 kervala
45 1 kervala
    // ... process some code here.
46 1 kervala
47 1 kervala
    // pause the timer..
48 1 kervala
    stopWatch.pause();
49 1 kervala
50 1 kervala
    // ... process some code that shouldn't affect your dynamically.
51 1 kervala
    // add the time we want to assume the above code took.
52 1 kervala
    stopWatch.addTime( /* time here is in ticks... */ 1500 );
53 1 kervala
54 1 kervala
    // resume the timer.
55 1 kervala
    stopWatch.resume();
56 1 kervala
57 1 kervala
    // ... process some code here.
58 1 kervala
59 1 kervala
    // stop the timer.
60 1 kervala
    stopWatch.stop();
61 1 kervala
62 1 kervala
    // start the timer.
63 1 kervala
    stopWatch.start();
64 1 kervala
    // ... process the same code here.
65 1 kervala
    // stop the timer.
66 1 kervala
    stopWatch.stop();
67 1 kervala
68 1 kervala
    // and output the results.
69 1 kervala
    nlinfo("Time Elapsed in Milliseconds: %d", stopWatch.getDuration());
70 1 kervala
    nlinfo("Average Time of Both start/stop calls in Milliseconds: %d", stopWatch.getAverageDuration());
71 1 kervala
}</code></pre>
72 1 kervala
73 1 kervala
h2. CHTimer in hierarchical_timer.h
74 1 kervala
75 1 kervala
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.
76 1 kervala
77 1 kervala
* Create a function to activate/deactivate the bench.
78 1 kervala
79 1 kervala
<pre><code class="CPP">void switchBench()
80 1 kervala
{
81 1 kervala
    static bool benching = false;
82 1 kervala
    benching = !benching;
83 1 kervala
    if(benching)
84 1 kervala
    {
85 1 kervala
        nlinfo("Start of benchmark");
86 1 kervala
        CHTimer::startBench();
87 1 kervala
    }
88 1 kervala
    else
89 1 kervala
    {
90 1 kervala
        nlinfo("End of benchmark");
91 1 kervala
        CHTimer::endBench();
92 1 kervala
        CHTimer::display(InfoLog, CHTimer::TotalTime, true, true);
93 1 kervala
        CHTimer::displayHierarchicalByExecutionPathSorted(InfoLog, CHTimer::TotalTime, true, 64, 3);
94 1 kervala
    }
95 1 kervala
}</code></pre>
96 1 kervala
97 1 kervala
* Then add H_BEFORE and H_AFTER around each code block you want to bench.
98 1 kervala
99 1 kervala
<pre><code class="CPP">H_BEFORE( nameofbenchinlist );
100 1 kervala
// ... your code to bench ...
101 1 kervala
H_AFTER( nameofbenchinlist );</code></pre>
102 1 kervala
103 1 kervala
* If you use H_AUTO, it'll bench the current scope.
104 1 kervala
105 1 kervala
<pre><code class="CPP">void func()
106 1 kervala
{
107 1 kervala
  H_AUTO( func ); // it'll bench the func() scope
108 1 kervala
  // ...some code...
109 1 kervala
  if(i<j)
110 1 kervala
  {
111 1 kervala
    H_AUTO( func_less ); // it'll bench the if() scope
112 1 kervala
    // ...some code...
113 1 kervala
  }
114 1 kervala
  // ...some code...
115 1 kervala
}</code></pre>