Robotics C++ Java AP Java Electronics  Summer   Physics II AP Physics Astronomy Other

 

C++ Timing Operations

For ease of use, all code is placed in one file

 

Example 1: The Class

Example 4: CPU Destroy

Exercises

 

Example 1

 

#include <time.h>
#include <iostream.h>

class SysTimer
{
public:

    SysTimer(); // constructor

    // methods to modify the state of the stopwatch

    void start();             // begin timing
    void stop();             // stop timing
    void reset();           // reset timer to 0; stops watch if running

    // returns true if and only if the stopwatch is currently running
   
bool isRunning() const;

    // returns number of microseconds since the stopwatch was started; if stopwatch is not

    // running, returns 0
   
double lapTime() const;

    // returns number of microseconds between last start and stop of stopwatch;
    // if stopwatch is running, returns 0

    double elapsedTime() const;

    // returns total of all times since stopwatch was last reset; if stopwatch is running, this includes

    // the current lap time
    double cumulativeTime() const;

    // returns the smallest time difference which can be measured by the stopwatch.
   
double granularity() const;

private:

    bool amRunning;                 // true iff stopwatch is currently running
    clock_t myStartTime;        // holds the last time the watch was started
    clock_t myElapsed;           // holds the time between the last start and the last stop

    // holds the total amount of time that the // stopwatch has been on since the last
    // reset (except for the current "lap" time)

    clock_t myCumulative;

};                                            // end class declaration

// Start class definition
SysTimer::SysTimer() : amRunning(false), myElapsed(0), myCumulative(0)
{ }

void SysTimer::start()
{
    if (amRunning)
    {
        cerr << "attempt to start an already running stopwatch" << endl;
    }
    else
    {
        amRunning = true;
        myStartTime = clock();
    }
}

void SysTimer::stop()
{
    if (! amRunning)
    {
        cerr << "attempt to stop a non-running stopwatch" << endl;
    }
    else
    {
        clock_t myEndTime = clock();
        myElapsed = myEndTime - myStartTime; // set the status variables
        myCumulative += myElapsed;
        amRunning = false; // turn off the stopwatch
    }
}

void SysTimer::reset()
{
    amRunning = false;
    myElapsed = 0;
    myCumulative = 0;
}

bool SysTimer::isRunning() const
{
    return amRunning;
}

// returns time in microseconds since last start
double SysTimer::lapTime() const
{
return amRunning ? (double)(clock() - myStartTime) / CLOCKS_PER_SEC : 0.0;
}

// returns time in microseconds between last stop and start
double SysTimer::elapsedTime() const
{
    return amRunning ? 0.0 : (double)myElapsed / CLOCKS_PER_SEC;
}

// returns total time stopwatch has been active
double SysTimer::cumulativeTime() const
{
    return ((double)myCumulative / CLOCKS_PER_SEC) + lapTime();
}

double SysTimer::granularity() const
{
    return 1.0 / CLOCKS_PER_SEC;
}            // End class definition

// Start Driver
int main()
{
    SysTimer measure;      // Create object of SysTimer class

    double m;
    long i;

    if (measure.isRunning())
    {
        cout << "Error: timer started out running" << endl;
    }

    // Time a loop doing one million multiplications
    measure.start();
    for (i=0; i<10000000; i++)
    {
        m = 3.5*5.3;
        if (5000000 == i) //Set as 1/2 above interval
        {
            cout << "At the halfway point, we have used " << measure.lapTime() << " seconds." << endl;
        }
    }
    measure.stop();

    cout << "Total loop took " << measure.elapsedTime() << " seconds." << endl;

    measure.reset();
    cout << "After reset timer now reads " << measure.elapsedTime() << " seconds." << endl;

    return 0;
}                          // End Driver

 

 

 

Example 2

 

#include "stdafx.h"

#include <math.h>

#include <time.h>

#include <iostream>

using namespace std;

 

class CTimer

{

private:

    time_t m_current_time;

    time_t m_seconds_elapsed;

    time_t m_start_time;

 

    bool m_paused;

    bool m_is_running;

 

public:

    CTimer();

    void reset();

    void start();

    void stop();

    long getElapsedSecs();

};

 

 

CTimer::CTimer()

{

    m_paused = false;

    m_is_running = false;

    m_seconds_elapsed = 0;

    m_start_time = 0;

    m_current_time = 0;

}

 

void CTimer::start()

{

    if( !m_is_running )

    {

        m_is_running = true;

        m_start_time = time(0);

    }

}

 

void CTimer::reset()

{

    if( !m_is_running )

    {

        //reset the timer.

        m_start_time = time(0);

        m_seconds_elapsed = 0;

    }

    else

    {

        //stop then reset then start again.

        stop();

        reset();

        start();

    }   

}

 

void CTimer::stop()

{

    if( m_is_running )

    {

    if(m_start_time == 0)

    {

        m_start_time = time(0);

    }

 

        //stop the time, add elapsed time, reset the first flag

        m_current_time = time(0);

        m_seconds_elapsed += (m_current_time - m_start_time);

        m_start_time = time(0);

        m_is_running = false;

    }

}

 

long CTimer::getElapsedSecs()

{

    if( m_is_running )

    {

        //Note: stop() updates the elapsed seconds.

        stop();

        start();

    }

 

    return m_seconds_elapsed;

}

 

int main ()

{

    CTimer myTimer;

    myTimer.reset();

    myTimer.start();

    for (double i = 0; i< 1000000; i = i + .01)

    {

        double a = pow(i,.5);

    }

    myTimer.stop();

    long time = myTimer.getElapsedSecs();

    cout<<"This problem took "<<time<<" seconds to run"<<endl;

    return 0;

}

 

 

 

Example 3

If you just want the program to pause until the user wants to continue

 

 

#include <iostream>
#include<string>
using namespace std;
int main()
{
    //some code
    string string1;
    cout<<"press return twice to continue";
    getline(cin, string1);          
    return 0;
}

 

Example 4

 

#include "stdafx.h"

#include <time.h>

#include <iostream>

using namespace std;

 

void wait(int seconds);

 

int main ()

{

  cout<<"You are not following instructions"<<endl;

  cout<<"In 5 seconds I am going to destroy your CPU!"<<endl;

  cout<< ("Starting countdown...\n");

  for (int n = 5; n>0; n--)

  {

    cout<<n<<endl;

    wait (1);

  }

  cout<<"Please wait, CPU being destroyed..."<<endl;

  return 0;

}

void wait (int seconds)

{

   clock_t endwait;

   endwait = clock () + seconds * CLK_TCK ;

   while (clock() < endwait);

}