| 1 | | 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. |
| 2 | | |
| 3 | | |
| 4 | | |
| 5 | | \begin{DocInclude}\begin{verbatim}#include <itpp/itbase.h> |
| 6 | | |
| 7 | | using namespace itpp; |
| 8 | | |
| 9 | | //These lines are needed for use of cout and endl |
| 10 | | using std::cout; |
| 11 | | using std::endl; |
| 12 | | |
| 13 | | int main() |
| 14 | | { |
| 15 | | //Declare the scalars used: |
| 16 | | long i, sum, N; |
| 17 | | |
| 18 | | //Declare tt as an instance of the timer class: |
| 19 | | Real_Timer tt; |
| 20 | | |
| 21 | | //Initiate the variables: |
| 22 | | N = 1000000; |
| 23 | | sum = 0; |
| 24 | | |
| 25 | | //Start and reset the timer: |
| 26 | | tt.tic(); |
| 27 | | |
| 28 | | //Do some processing |
| 29 | | for (i = 0; i < N; i++) { |
| 30 | | sum += i; |
| 31 | | } |
| 32 | | |
| 33 | | // Print the elapsed time |
| 34 | | tt.toc_print(); |
| 35 | | |
| 36 | | //Print the result of the processing: |
| 37 | | cout << "The sum of all integers from 0 to " << N - 1 << " equals " << sum << endl; |
| 38 | | |
| 39 | | //Exit program: |
| 40 | | return 0; |
| 41 | | |
| 42 | | } |
| 43 | | \end{verbatim} |
| 44 | | \end{DocInclude} |
| 45 | | |
| 46 | | |
| 47 | | When you run this program, the output will look something like this: |
| 48 | | |
| 49 | | |
| 50 | | |
| 51 | | \begin{DocInclude}\begin{verbatim}Elapsed time = 0.000797055 seconds |
| 52 | | The sum of all integers from 0 to 999999 equals 1783293664 |
| 53 | | \end{verbatim} |
| 54 | | \end{DocInclude} |
| 55 | | |