Click here to Skip to main content
15,897,519 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.7K   403   17  
Time how long an application or a set of processes run for, to millisecond precision.
//
// example.cpp : Shows example ways in which the timer class can be used. File is also used as an
// unit test of the object. Application is CLI based.
//
// Notes: This program uses the Sleep() function in windows.h to pause the application for certain
// periods of time, which tends not to be incredibly accurate. This means that times may be off by
// a few milliseconds when computing.
//
// This example program has been compiled successfully with "Microsoft Visual Studio .NET 2003,"
// "Microsoft Visual Studio 2005," and "Dev-C++" under the windows operating systems. This
// application has not been tested with any other compilers.
//
// Made by: Robert Roose
//          summatix
//

#include <iostream>
#include <string>
#include <windows.h> //windows.h is used for its Sleep() function
#include <sstream>
#include "timer.h" //Include the timer class

using namespace std;

int main()
{
    //Start of application
    //
    //Start of Test 1
    //
    cout << "Test 1\n" << "============" << endl;
    timer timerA; //Create a new timer instance
    cout << "Starting the Timer...\n";
    timerA.start(); //Start the timer
    cout << "Pausing the program for 2 seconds...\n";
    Sleep(2000); //Pause the application for 2 seconds
    timerA.stop(); //Stop the timer
    cout << "Timer Stopped\n";

    //Output how long the timer instance has been running
    cout << "The application has been running for: " << timerA << "\n";
    cout << "The output should have been: 2secs\n";

    cout << "Test 1 finished\n\n" << endl;
    //
    //End of Test 1

    //Start of Test 2
    //
    cout << "Test 2\n" << "============" << endl;
    cout << "Starting the timer again, and then pausing for 2.5 seconds\n";
    timerA.start(); //Start the timer again
    Sleep(2500); //Pause the application for 2.5 seconds
    timerA.stop(); //Stop the timer
    cout << "Timer Stopped\n";

    //Output how long the timer instance has been running
    cout << "The application has been running for: " << timerA << "\n";
    cout << "The output should have been: 4.5secs\n";

    cout << "Test 2 finished\n\n" << endl;
    //
    //End of Test 2

    //Start of Test 3
    //
    cout << "Test 3\n" << "============" << endl;
    cout << "Restarting the timer, and then pausing for 1.515 seconds\n";
    timerA.restart(); //Restart the timer
    Sleep(1515); //Pause the application for 1.523 seconds
    timerA.stop(); //Stop the timer
    cout << "Timer Stopped\n";

    //Output how long the timer instance has been running
    cout << "The application has been running for: " << timerA << "\n";
    cout << "The output should have been: 1.515secs\n";

    //Store the running length of the timer instance as a string type
    string testString = timerA;
    cout << "Outputting string\n";

    //Output how long the timer instance has been running
    cout << "The application has been running for: " << testString << "\n";
    cout << "The output should have been: 1.515secs\n";

    //Store the number of seconds the timer has been running for in an int data type
    int testInt = timerA;
    cout << "Outputting int\n";

    //Output how long the timer instance has been running
    cout << "The application has been running for: " << testInt << "\n";
    cout << "The output should have been: 1\n";

    //Store the number of seconds the timer has been running for in a double data type
    double testDouble = timerA;
    cout << "Outputting double\n";
 
    //Output how long the timer instance has been running
    cout << "The application has been running for: " << testDouble << "\n";
    cout << "The output should have been: 1.515\n";

    cout << "Test 3 finished\n\n" << endl;
    //
    //End of Test 3

    //Start of Test 4
    //
    cout << "Test 4\n" << "============" << endl;
    cout << "Creating a new timer, starting instantly, and then pausing for 1 hour, 23 minutes,"
         << " and 6 seconds.\n";

    timer timerB(true); //Create a new timer, starting instantly
    Sleep(4986000); //Pause application for 1 hour, 23 minutes and 6 seconds
    timerB.stop(); //Stop the timer

    timerB.setClockType(cpuClock); //Set the timer to use the cpu clock

    //Output how long the timer instance has been running using the cpu clock
    cout << "Using cpu clock, the application has been running for: " << timerB << "\n";
    cout << "The output should have been: 1hr 23mins 6secs\n";

    timerB.setClockType(systemClock); //Set the timer to use the system clock

    //Output how long the timer instance has been running using the systen clock
    cout << "Using system clock, the application has been running for: " << timerB << "\n";
    cout << "The output should have been: 1hr 23mins 6secs\n";

    cout << "Test 4 finished\n\n" << endl;
    //
    //End of Test 4*/

    //
    //End of application

    system("PAUSE");
    return 0;
}

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