Tag Archives: timer
C++ || Snippet – Simple Timer Class Using The Clock Function
The following is sample code for a simple timer class using the “clock()” function. The “clock” function returns the processor time that has been consumed by the program during its execution; or in other words, it returns the number of clock ticks that has elapsed since the program was launched.
The timer class demonstrated on this page has the ability to measure the execution time of a block of code or function call, and display the elapsed running time (in seconds) to the screen.
Unfortunately, by using the clock() function (instead of date based methods) to measure time, this class is only able to measure the time that was actually spent used by the CPU during the execution of the program. That means that if this timer class was used to measure how long it took for a user to enter data into the program, the users CPU wouldn’t notice any change in time, and this timer class would display that 0 seconds had elapsed.
However, by using the clock() function to measure elapsed time, it can be used to implement a simple and portable timer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
// ============================================================================ // Author: K Perkins // Date: Jul 22, 2013 // Taken From: http://programmingnotes.org/ // File: Timer.h // Description: This is a simple timer class using the "clock" function. // ============================================================================ #ifndef TIMER_H #define TIMER_H #include <cassert> #include <ctime> class Timer { public: Timer(); /* Function: Constructor initializes data Precondition: None Postcondition: Defines private variables */ void Start(); /* Function: Starts the timer Precondition: Timer has been initialized Postcondition: Timer has been started */ void Stop(); /* Function: Stops the timer Precondition: Timer has been initialized Postcondition: Timer has been stopped */ void Reset(); /* Function: Resets the clock Precondition: Timer has been initialized Postcondition: Timer has been restarted */ double Timestamp() const; /* Function: Returns the amount of time (in seconds) that has elapsed since the clock has started. Precondition: The timer has been started but does not need to be stopped. The timer continues to run Postcondition: The total time since the clock started is returned */ double Elapsed() const; /* Function: Returns the amount of time (in seconds) that has elapsed since the clock has started. Precondition: The timer has been started and must be stopped Postcondition: The total elapsed time after the clock has stopped is returned */ private: clock_t m_start; // the first reading by the clock clock_t m_end; // the last reading by the clock }; #endif //========================= Implementation ================================// Timer::Timer() { Reset(); }// end of Timer void Timer::Start() { m_start = clock(); }// end of Start void Timer::Stop() { m_end = clock(); }// end of Stop void Timer::Reset() { m_start = 0; m_end = 0; }// end of Reset double Timer::Timestamp() const { clock_t temp = clock(); assert(temp >= m_start); return (double)(temp-m_start)/((double)CLOCKS_PER_SEC); }// end of Timestamp double Timer::Elapsed() const { assert(m_end >= m_start); return (double)(m_end-m_start)/((double)CLOCKS_PER_SEC); }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
===== DEMONSTRATION HOW TO USE =====
Use of the above class is very simple to use. Here are sample programs demonstrating its use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <iostream> #include <cstdlib> #include <ctime> #include <algorithm> #include "Timer.h" using namespace std; // function prototype void Display(int arry[], int size); // constant variable const int SIZE = 10000000; int main() { // declare variables srand(time(NULL)); Timer timer; int* arry = new int[SIZE]; // place random numbers into the array for(int x = 0; x < SIZE; ++x) { arry[x] = rand()%1000+1; } // display array data if within range if(SIZE <= 20) { cout<<"Original array values:n"; Display(arry, SIZE); } cerr<<"nStarting the timer.."; // start the timer timer.Start(); // sort the array using the built in function std::sort(arry, arry+SIZE); // stop the timer timer.Stop(); cout<<"nTimer stopped!n"; // display array data if within range if(SIZE <= 20) { cout<<"nThe current sorted array:n"; Display(arry, SIZE); } // display the elapsed time cout<<"nIt took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; return 0; }// end of main void Display(int arry[], int size) { for(int x = 0; x < size; ++x) { cout<<arry[x]<<" "; } cout<<"n"; }// http://programmingnotes.org/ |
SAMPLE OUTPUT:
Starting the timer..
Timer stopped!It took 3749 clicks (3.749 seconds)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include "Timer.h" using namespace std; int main() { // declare variables Timer timer; int sleep = 0; int elapsedtime = 0; // ask user how long they want to sleep cout<< "How many seconds do you want to sleep?: "; cin >> sleep; cerr<<"nStarting the timer..n"; // start the timer timer.Start(); // use a loop to sleep while((int)timer.Timestamp() < sleep) { if((int)timer.Timestamp() == elapsedtime) { cout<<elapsedtime++<<endl; } } // stop the timer timer.Stop(); cout<<"naTimer stopped!n"; // display the elapsed time cout<<"nIt took "<<timer.Elapsed()*1000 <<" clicks ("<<timer.Elapsed()<<" seconds)"<<endl; return 0; }// http://programmingnotes.org/ |
SAMPLE OUTPUT:
How many seconds do you want to sleep?: 5
Starting the timer..
0
1
2
3
4Timer stopped!
It took 5000 clicks (5 seconds)