Timer class for C/C++ – run on all Linux/Windows systems

File  header timer.h:

#ifndef TIMER_H
#define TIMER_H
#include <time.h>
class Timer
{
public:
Timer();
~Timer();
void start();
float Get_Elapsed_restart();
float Get_Elapsed();
float Get_Elapsed_s();
float Get_Elapsed_s_restart();
private:
clock_t starttime, finaltime;
};
#endif
File timer.cpp:

#include “timer.h”
Timer::Timer()
{
}
Timer::~Timer()
{
}

void Timer::start()
{
starttime=clock();
}

float Timer::Get_Elapsed_restart()
{
finaltime=clock();
float val=(float)(finaltime-starttime);
starttime=finaltime;
return val;
}

float Timer::Get_Elapsed()
{
finaltime=clock();
return (float)(finaltime-starttime);
}

float Timer::Get_Elapsed_s()
{
return (float)Get_Elapsed()/CLOCKS_PER_SEC;
}

float Timer::Get_Elapsed_s_restart()
{
return (float)Get_Elapsed_restart()/CLOCKS_PER_SEC;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: