Time taken by your program

Lately I am trying to solve programming puzzles. One thing these various programming puzzles asks is the resource efficiencies and speed. Unix provides time command to calculate the time taken by your program, but does not give you option for the time taken at various stage of your program. So I wrote simple timer class.

Here is the code.

#ifndef TIMER_H
#define TIMER_H

#include <ctime>
#include <string>
#include <sys/times.h>
#include <unistd.h>  

class Timer {
public:
	Timer();
	Timer(const std::string &label);
	~Timer();
	void start();
	void stop();
	void show();
	double get_user_time();
	double get_system_time();
	double get_wait_time();
	double get_elapsed_time();
	double get_non_wait_time();
private:
	void reset();
	std::string label;
	long tps;
	long start_time, end_time;
	double usertime, systemtime, elapsedtime, waittime;
	struct tms start_cpu_time, end_cpu_time;
};

#endif

#include <iostream>
#include "Timer.h"

Timer::Timer () {
	label = "Process Timer";
	reset();
}
Timer::Timer (const std::string &label) {
	Timer::label = label;
	reset();
	start();
}

Timer::~Timer() {
}

void Timer::reset() {
	tps = sysconf ( _SC_CLK_TCK );
	end_time = 0;
	usertime = 0;
	systemtime = 0;
	elapsedtime = 0;
	waittime = 0;
}
void Timer::start() {
	start_time = times(&(start_cpu_time));
}

void Timer::show() {
	std::cout 
		<< "  "
		<< label << "\n"
		<< "  -------------------------------\n"
		<< "  User CPU Time  : "
		<< usertime << " s\n"
		<< "  System CPU Time: "
		<< systemtime << " s\n"
		<< "  Wait Time      : "
		<< waittime << " s\n"
		<< "  -------------------------------\n"
		<< "  Elapsed Time   : "
		<< elapsedtime << " s\n" << std::endl;
}
void Timer::stop() {
	end_time = times(&end_cpu_time);
	elapsedtime = ((double)(end_time - start_time )/(double)tps );
	if (elapsedtime < 0.001) {
		elapsedtime = 0.001;
	}
	usertime = ((double)(end_cpu_time.tms_utime - start_cpu_time.tms_utime)/(double)tps);
	systemtime = ((double)(end_cpu_time.tms_stime - start_cpu_time.tms_stime)/(double)tps);
	waittime = (elapsedtime - (usertime + systemtime));
	if ( waittime < 0.00 ) {
		waittime = 0.00;
	}
	show();
}
double Timer::get_user_time(void) {
	return usertime;
}
double Timer::get_system_time(void) {
	return systemtime;
}
double Timer::get_wait_time(void) {
	return waittime;
}
double Timer::get_non_wait_time(void) {
	return usertime + systemtime;
}
double Timer::get_elapsed_time(void) {
	return elapsedtime;
}

Create a object of Timer class before your function call and call stop method , you will get output as follows:
Main Program Finished in

  -------------------------------
  User CPU Time  : 0 s
  System CPU Time: 0 s
  Wait Time      : 0.001 s
  -------------------------------
  Elapsed Time   : 0.001 s
Advertisements

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