Click here to Skip to main content
15,886,833 members
Articles / Desktop Programming / MFC

Timer class - Time how long your programs run for

Rate me:
Please Sign up or sign in to vote.
1.49/5 (12 votes)
20 Dec 2007CPOL1 min read 34.6K   403   17  
Time how long an application or a set of processes run for, to millisecond precision.
//
// timer.h : Declares the timer class. The class is used for determining the running length of
// an application or proccess of tasks. This class is accurate to the 0.001 of a second.
//
// The interface includes a start() method to begin the timer and a stop() method. When a timer is
// stopped, the running length is added to the accumalated result, allowing for the timer to be
// started again to further increase the running length of the timer. The restart() method stops
// the timer, and resets the accumalated time back to 0.
//
// There are several ways in which to receive the running length of the timer. The '<<' operator
// is overloaded so an instance of the class can be called directly using an ostream to output the
// running length of the timer. eg. std::cout << timerInstance << std::endl;. An instance can also
// be directly converted to a string, int or double. eg. int intVariable = timerInstance,
// std::string stringVariable = timerInstance. The string conversion will give the same format
// as using the '<<' operator (eg. 2hrs 20mins 27secs) wheras the int and double will give the
// number of seconds the timer has been running for. Alternatively, the method getNoOfSecs()
// returns a double data type with the same result as the double overloaded method.
// Note: converting instance to int will lose precision of time.
//
// A timer instance can be started instantly upon creation of a timer object by using the
// constructor timer(true) to initiate the instance.
//
// This class also includes a useful method: convertToString( int or double ) which converts an
// int or double data type to a string type variable. This method has been made static, so that
// an instance of the class does not have to have been created in order to use the function.
//
// See example.cpp for more information on using the timer class.
//
// Made by: Robert Roose
//          summatix
//

#ifndef TIMER_H
#define TIMER_H

#include <ctime>
#include <iostream>
#include <iomanip>
#include <string>

// Define the allowable values for the Clock type
//
enum type
{
    cpuClock,
    systemClock,
    standard
};
//

class Timer
{
    public:
        // Constructors and destructors
        //
        Timer(); //Defualt
        Timer(const bool& start); //Initially set the timer as started by issuing timer(true)
        //

        // Class Methods
        //
        virtual void start(); //Starts the timer, but does nothing if the timer is already running
        virtual void reset(); //Restarts the timer, but does nothing if the timer has not started
        virtual void stop(); //Stops the timer

        //Returns the number of seconds the timer has been running for
        virtual double getNoOfSecs() const;

        //Sets the type of time algorithm to use. The standard clock type uses the cpu clock if
        //the time difference is less than 1 hour, otherwise it uses the system time.
        virtual void setClockType(const type& type);
        //

        // Conversion methods
        //

        //Clients can store the running length of the timer directly into a string variable by
        //'std::string variableName = timerInstance'
        virtual operator std::string() const;

        //Clients can store the number of seconds the timer has been running for directly into an
        //int variable by 'int variableName = timerInstance'
        virtual operator int() const;

        //Clients can store the number of seconds the timer has been running for directly into an
        //double variable by 'double variableName = timerInstance'
        virtual operator double() const;

        //Methods to convert values to strings
        //
        static std::string convertToString(const int& valueToBeConverted);
        static std::string convertToString(const double& valueToBeConverted);
        //

        //

    protected:
        //Helper method to determine how long the timer has been running for
        virtual double elapsedTime() const;

        //Client can use '<<' to output the total running length of the timer
        friend std::ostream& operator<<(std::ostream& os, Timer& t);

        //Define variables
        bool running;
        clock_t startClock;
        time_t startTime;
        double accTime;
        type clockType;

    private:
        //Disable the option for clients to use the copy constructor and assignment operator
        //
        Timer(const Timer& src);
        Timer& operator=(const Timer& rhs);
        //
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions