Click here to Skip to main content
13,410,470 members (35,299 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 19 Oct 2007

Timer class - Time how long your programs run for

, 20 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Time how long an application or a set of processes run for, to millisecond precision.


This timer class is used for determining how long a set of processes run for, to the precision of milliseconds. This can be useful if you want to benchmark PCs or want to know whether changing some code in your application affects the speed at which it processes.


I created this class because I could not find another class or set of functions which could time how long it took for my applications to run. After creating the initial object class for the timer, I then expanded on it to create the class which I am describing now.

Using the code

The interface consists of one object: timer. In order to use this class, you need to create an instance of the object using either the default constructor or the boolean constrictor.

To create a timer instance, use one of the following statements:

timer newTimerInstance(); //Create a timer instance

timer newTimerInstance(true); //Use this constructor to start the timer as
                              //soon as the object is created

You can start, stop, and restart the timer by using the following statements:

newTimerInstance.start(); //Start the timer

newTimerInstance.stop(); //Stop the timer

newTimerInstance.start(); //Start the timer again after having been
                          //stopped the timer previously, continuing
                          //accumulating the time

To reset the accumulated time:

//This sets the timer back to 0 so it can be restarted again using start()

You can output the running length of the timer directly using "<<" or store the object as a string, int, or double:

std::cout << newTimerInstance; //Output the running length of the timer

std::string newString = newTimerInstance; //Store the running length of the
                                          //timer as a string

int newInt = newTimerInstance; //Store the number of seconds which the timer
                               //has been running for in an int
double newDouble = newTimerInstance; //Store the number of seconds which the
                                     //timer has been running for in a double

For more information on how to use the class, see the example.cpp included with the package.


  • Timer class 1.1 - API modified to represent more closely to a traditional timer. The Restart method has been replaced with Reset. MS naming conventions for objects now used.
  • Timer class 1.2 - API redesigned, hopefully making the usage easier. Pause and Resume methods have been added, and the functionality of the Start and Stop methods have been modified. This makes the timer concept more intuitive.

For more information, visit:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRE: Good work + some humble suggestions Pin
Robert Roose16-Dec-07 12:47
memberRobert Roose16-Dec-07 12:47 
GeneralRe: RE: Good work + some humble suggestions Pin
Pepsibot19-Dec-07 15:08
memberPepsibot19-Dec-07 15:08 
GeneralRe: RE: Good work + some humble suggestions Pin
Robert Roose20-Dec-07 16:04
memberRobert Roose20-Dec-07 16:04 
GeneralRe: RE: Good work + some humble suggestions Pin
Pepsibot25-Dec-07 22:15
memberPepsibot25-Dec-07 22:15 
GeneralGood work + some humble suggestions Pin
Pepsibot11-Dec-07 22:47
memberPepsibot11-Dec-07 22:47 
GeneralCoding Conventions Pin
Ri Qen-Sin10-Dec-07 1:44
memberRi Qen-Sin10-Dec-07 1:44 
GeneralRe: Coding Conventions Pin
Pepsibot11-Dec-07 22:53
memberPepsibot11-Dec-07 22:53 
Give the author a break. Maybe he writes php or perl or java and doesn't use MS coding conventions. .NET is not the only language available.
GeneralRe: Coding Conventions Pin
Robert Roose16-Dec-07 12:55
memberRobert Roose16-Dec-07 12:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180221.1 | Last Updated 20 Dec 2007
Article Copyright 2007 by summatix
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid