common/clocktimer.c
///////////////////////////////////////////////////////////////////////////////
// Filename: clocktimer.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: implementation of class "clocktimer", a class that measures
// real time
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date Time Name Description
// -------- -------- -------- ------------------------------------------------
// 96/02/06 01:30:29 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
/* NONE */
// System headers /////////////////////////////////////////// System headers //
/* NONE */
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
// Macros /////////////////////////////////////////////////////////// Macros //
/* NONE */
// File scope objects /////////////////////////////////// File scope objects //
/* NONE */
// External variables, functions, and classes ///////////// External objects //
/* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
/* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
/* NONE */
// Functions and class implementation /// Functions and class implementation //
/*
* start timer
*/
bool
clocktimer::start(void)
{
gottime = false;
if(gettimeofday(&tstart, NULL) != 0)
{
cerr << "clocktimer::start:gettimeofday() error" << endl;
return false;
}
started = true;
return true;
}
/*
* stop timer and calculate result
*/
bool
clocktimer::end(void)
{
timeval tend;
if(!started)
{
cerr << "clocktimer::end(): called without calling"
<< " clocktimer::start() first!" << endl;
return false;
}
if(gettimeofday(&tend, NULL) != 0)
{
cerr << "clocktimer::end():gettimeofday() error" << endl;
return false;
}
if(tend.tv_usec < tstart.tv_usec)
{
int nsec = (tstart.tv_usec - tend.tv_usec) / 1000000 + 1;
tstart.tv_usec -= 1000000 * nsec;
tstart.tv_sec += nsec;
}
if(tend.tv_usec - tstart.tv_usec > 1000000)
{
int nsec = (tstart.tv_usec - tend.tv_usec) / 1000000;
tstart.tv_usec += 1000000 * nsec;
tstart.tv_sec -= nsec;
}
tresult.tv_sec = tend.tv_sec - tstart.tv_sec;
tresult.tv_usec = tend.tv_usec - tstart.tv_usec;
gottime = true;
started = false;
return true;
}
/*
* return number of measured seconds or -1 for an error
*/
long
clocktimer::secs(void)
{
if(gottime)
return tresult.tv_sec;
else
return -1;
}
/*
* return number of measured microseconds or -1 for an error
*/
long
clocktimer::usecs(void)
{
if(gottime)
return tresult.tv_usec;
else
return -1;
}
// Main /////////////////////////////////////////////////////////////// Main //
/* NONE */