Click here to Skip to main content
Click here to Skip to main content

Timer class - Time how long your programs run for

By , 20 Dec 2007
 

Introduction

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.

Background

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:

newTimerInstance.reset();
//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.

History

  • 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: www.summatix.com.

License

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

About the Author

summatix
Australia Australia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRE: Good work + some humble suggestionsmemberRobert Roose16 Dec '07 - 11:47 
GeneralRe: RE: Good work + some humble suggestionsmemberPepsibot19 Dec '07 - 14:08 
Would you be willing to consider a pause function? I understand, by what you've said as well as by reading the code, that the same concept can be handled with certain calls. However, I can't help but feel like there are some additional components you could add that would make the entire concept more intuitive. How about a pause function, coupled with either a resume or continue function, that, for as long as the running flag is marked, halts or re-enables accumulation of time? I think if you included that as a feature then it would be a very valuable resource for many developers.
 
Thanks.
GeneralRe: RE: Good work + some humble suggestionsmemberRobert Roose20 Dec '07 - 15:04 
GeneralRe: RE: Good work + some humble suggestionsmemberPepsibot25 Dec '07 - 21:15 
GeneralGood work + some humble suggestionsmemberPepsibot11 Dec '07 - 21:47 
GeneralCoding ConventionsmemberRi Qen-Sin10 Dec '07 - 0:44 
GeneralRe: Coding ConventionsmemberPepsibot11 Dec '07 - 21:53 
GeneralRe: Coding ConventionsmemberRobert Roose16 Dec '07 - 11:55 

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

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