1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
//
// Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
//
// This file may be distributed under terms of the GPL
//
//
// $Id: timer.h,v 1.4 1997/07/18 03:32:35 bgrayson Exp $
//
#ifndef _TIMER_H_
#define _TIMER_H_
#define TIMER_H_CVSID "$Id: timer.h,v 1.4 1997/07/18 03:32:35 bgrayson Exp $"
//
// General purpose interval timer class
//
// Implemented using BSD derived function gettimeofday for greater resolution
//
// Author : Mike Romberg
#include "timeval.h"
class Timer {
public:
Timer( int start = 0 ) { if ( start ) Timer::start(); }
~Timer( void ){}
void start( void ) { gettimeofday( &starttime_, NULL ); }
void stop( void ) { gettimeofday( &stoptime_, NULL ); }
// reports time intervall between calls to start and stop in usec
long long report( void ) const {
return (stoptime_.tv_sec - starttime_.tv_sec) * 1000000
+ stoptime_.tv_usec - starttime_.tv_usec;
}
ostream &printOn(ostream &os) const {
return os <<"Timer : ["
<<"starttime_ = " <<TimeVal(starttime_)
<<", stoptime_ = " <<TimeVal(stoptime_)
<<", duration = " <<report() <<"]";
}
protected:
struct timeval starttime_, stoptime_;
private:
};
inline ostream &operator<<(ostream &os, const Timer &t){
return t.printOn(os);
}
#endif
|