1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
---|
3 | <title>mixpp: Using timers to measure execution time</title> |
---|
4 | <link href="tabs.css" rel="stylesheet" type="text/css"> |
---|
5 | <link href="doxygen.css" rel="stylesheet" type="text/css"> |
---|
6 | </head><body> |
---|
7 | <!-- Generated by Doxygen 1.5.9 --> |
---|
8 | <script type="text/javascript"> |
---|
9 | <!-- |
---|
10 | function changeDisplayState (e){ |
---|
11 | var num=this.id.replace(/[^[0-9]/g,''); |
---|
12 | var button=this.firstChild; |
---|
13 | var sectionDiv=document.getElementById('dynsection'+num); |
---|
14 | if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){ |
---|
15 | sectionDiv.style.display='block'; |
---|
16 | button.src='open.gif'; |
---|
17 | }else{ |
---|
18 | sectionDiv.style.display='none'; |
---|
19 | button.src='closed.gif'; |
---|
20 | } |
---|
21 | } |
---|
22 | function initDynSections(){ |
---|
23 | var divs=document.getElementsByTagName('div'); |
---|
24 | var sectionCounter=1; |
---|
25 | for(var i=0;i<divs.length-1;i++){ |
---|
26 | if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){ |
---|
27 | var header=divs[i]; |
---|
28 | var section=divs[i+1]; |
---|
29 | var button=header.firstChild; |
---|
30 | if (button!='IMG'){ |
---|
31 | divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild); |
---|
32 | button=document.createElement('img'); |
---|
33 | divs[i].insertBefore(button,divs[i].firstChild); |
---|
34 | } |
---|
35 | header.style.cursor='pointer'; |
---|
36 | header.onclick=changeDisplayState; |
---|
37 | header.id='dynheader'+sectionCounter; |
---|
38 | button.src='closed.gif'; |
---|
39 | section.id='dynsection'+sectionCounter; |
---|
40 | section.style.display='none'; |
---|
41 | section.style.marginLeft='14px'; |
---|
42 | sectionCounter++; |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | window.onload = initDynSections; |
---|
47 | --> |
---|
48 | </script> |
---|
49 | <div class="navigation" id="top"> |
---|
50 | <div class="tabs"> |
---|
51 | <ul> |
---|
52 | <li><a href="main.html"><span>Main Page</span></a></li> |
---|
53 | <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> |
---|
54 | <li><a href="annotated.html"><span>Classes</span></a></li> |
---|
55 | <li><a href="files.html"><span>Files</span></a></li> |
---|
56 | </ul> |
---|
57 | </div> |
---|
58 | <div class="navpath"><a class="el" href="dev_guide.html">BDM Use - in C++</a> |
---|
59 | </div> |
---|
60 | </div> |
---|
61 | <div class="contents"> |
---|
62 | <h1><a class="anchor" name="timer">Using timers to measure execution time </a></h1>In this example we are using the Real_Timer class to measure the execution time of a simple program. The Real_Timer class is included in the itmisc library.<p> |
---|
63 | <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <itpp/itbase.h></span> |
---|
64 | |
---|
65 | <span class="keyword">using namespace </span>itpp; |
---|
66 | |
---|
67 | <span class="comment">//These lines are needed for use of cout and endl</span> |
---|
68 | <span class="keyword">using</span> std::cout; |
---|
69 | <span class="keyword">using</span> std::endl; |
---|
70 | |
---|
71 | <span class="keywordtype">int</span> main() { |
---|
72 | <span class="comment">//Declare the scalars used:</span> |
---|
73 | <span class="keywordtype">long</span> i, sum, N; |
---|
74 | |
---|
75 | <span class="comment">//Declare tt as an instance of the timer class:</span> |
---|
76 | Real_Timer tt; |
---|
77 | |
---|
78 | <span class="comment">//Initiate the variables:</span> |
---|
79 | N = 1000000; |
---|
80 | sum = 0; |
---|
81 | |
---|
82 | <span class="comment">//Start and reset the timer:</span> |
---|
83 | tt.tic(); |
---|
84 | |
---|
85 | <span class="comment">//Do some processing</span> |
---|
86 | <span class="keywordflow">for</span> ( i = 0; i < N; i++ ) { |
---|
87 | sum += i; |
---|
88 | } |
---|
89 | |
---|
90 | <span class="comment">// Print the elapsed time</span> |
---|
91 | tt.toc_print(); |
---|
92 | |
---|
93 | <span class="comment">//Print the result of the processing:</span> |
---|
94 | cout << <span class="stringliteral">"The sum of all integers from 0 to "</span> << N - 1 << <span class="stringliteral">" equals "</span> << sum << endl; |
---|
95 | |
---|
96 | <span class="comment">//Exit program:</span> |
---|
97 | <span class="keywordflow">return</span> 0; |
---|
98 | |
---|
99 | } |
---|
100 | </pre></div><p> |
---|
101 | When you run this program, the output will look something like this:<p> |
---|
102 | <div class="fragment"><pre class="fragment">Elapsed time = 0.000797055 seconds |
---|
103 | The sum of all integers from 0 to 999999 equals 1783293664 |
---|
104 | </pre></div> </div> |
---|
105 | <hr size="1"><address style="text-align: right;"><small>Generated on Wed Oct 7 17:34:45 2009 for mixpp by |
---|
106 | <a href="http://www.doxygen.org/index.html"> |
---|
107 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> |
---|
108 | </body> |
---|
109 | </html> |
---|