0

Design a CPU Timer class (you may name it CPUTimer). This class should allow the user to start timer, stop timer, compute delta time and print the lapsed CPU time of any block of code in a program.

#pragma once
#include <iostream>
#include <cmath>
#include <ctime>
#ifndef CPUTimer_h
#define CPUTimer_h

using namespace std;


class CPUTimer
{
private:
    long t0;
    long t1;


public:
    CPUTimer(void); 
    ~CPUTimer(void);


    void startSetT0();
    long getT0();


    void stopSetT1();
    long getT1();


    void printTimeConsumed ();
};
#endif


int main ()
{
    CPUTimer counter;


    counter.startSetT0();


    for(float i=0; i<10000000; ++i) 
        pow(i,i) * sqrt(i*i) / exp(i);


    counter.stopSetT1();
    counter.getT1();
    counter.printTimeConsumed();


    return 0;
}




CPUTimer::CPUTimer(void)
{
}


CPUTimer::~CPUTimer(void)
{
}

void CPUTimer::startSetT0()
{
    t0=clock();
}

void CPUTimer::stopSetT1()
{
    t1 = clock ();
}

long CPUTimer:: getT0(){
    return t0;
}
long CPUTimer::getT1(){


    return t1-t0;
}




void CPUTimer::printTimeConsumed ()
{ 
    cout << "CPU times in ms below... " << endl << endl;
    cout << "CPU start time in ms ===> " << t0 << endl;
    cout << "CPU stop time in ms ====> " << t1 << endl;
    cout << "CPU running time change in ms ===>The change in CPU running time in ms = " << t1-t0 << endl << endl;
    system("pause");
};

Edited by Dani: Not a code snippet

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mike_2000_17
0

Imagine I'm the user of your class. I'm not interested in knowing about T0 or T1. I don't care about how the class keeps the time periods. All I want is a clear way to use the class, e.g., start, stop, and get elapsed time. Thus far, your implementation seem OK, but your class is confusing to use. Why do you need to bother the user with the T0 and T1 business. Consider simply renaming your functions: startSetT0 -> Start, stopSetT1 -> Stop, getT0 -> getStartTime, getT1 -> getStopTime, getT1 -> getElapsedTime, and printTimeConsumed -> printTimeInfo.

A lot of the art of OOP is to make classes with clear semantics (that are easy to use and understand) while hiding any implementation detail that the user doesn't have to know or care about (like t0 and t1 in this case). This can make all the difference in the world.

Also, your header starts with:

#pragma once
#include <iostream>
#include <cmath>
#include <ctime>
#ifndef CPUTimer_h
#define CPUTimer_h

the #pragma once is redundant with the #ifndef CPUTimer_h because both serve the same purpose, they are header-guards. And the includes should also be within the header-guard. Thus, you should just have:

#ifndef CPUTimer_h
#define CPUTimer_h
#include <iostream>
#include <cmath>
#include <ctime>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.