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