root/foundation-apps/grosview-maxx/timer.h

Revision 2, 2.0 KB (checked in by emasson, 3 years ago)

initial import for the community edition

Line 
1//
2//  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
3//
4//  This file may be distributed under terms of the GPL
5//
6//
7// $Id: timer.h,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $
8//
9#ifndef _TIMER_H_
10#define _TIMER_H_
11
12#define TIMER_H_CVSID "$Id: timer.h,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $"
13
14//
15//                 General purpose interval timer class
16//
17//  Implemented using BSD derived function gettimeofday for greater resolution
18//
19//   Author : Mike Romberg
20
21
22#include "timeval.h"
23
24class Timer {
25public:
26  Timer( int start = 0 ) { if ( start ) Timer::start(); }
27  ~Timer( void ){}
28
29  void start( void ) { gettimeofday( &starttime_, NULL ); }
30  void stop( void )  { gettimeofday( &stoptime_, NULL );  }
31  //  reports time intervall between calls to start and stop in usec
32  //  XXX  NOTE THAT THIS SUFFERS FROM OVERFLOW!
33  //       After 53 minutes, report() will return a negative number!
34  //       To be safe, we should use the one that returns a double.
35  //       Once the new function appears to work fine, this whole
36  //       region can be removed from the file.    bgrayson, 11/99
37#if 0
38  LONG_LONG report( void ) const {
39    err << "This function Timer::report() should no longer be used!\n";
40    exit(-1);
41    return (stoptime_.tv_sec - starttime_.tv_sec) * 1000000
42           + stoptime_.tv_usec - starttime_.tv_usec;
43  }
44#endif
45  //  This one uses doubles as the return value, to avoid
46  //  overflow/sign problems.
47  double report_usecs(void) const {
48    return (stoptime_.tv_sec - starttime_.tv_sec) * 1000000.0
49      + stoptime_.tv_usec - starttime_.tv_usec;
50  }
51
52  std::ostream &printOn(std::ostream &os) const {
53    return os <<"Timer : ["
54      <<"starttime_ = " <<TimeVal(starttime_)
55      <<", stoptime_ = " <<TimeVal(stoptime_)
56      <<", duration = " <<report_usecs() <<" usecs]";
57  }
58
59protected:
60  struct timeval starttime_, stoptime_;
61
62private:
63};
64
65inline std::ostream &operator<<(std::ostream &os, const Timer &t){
66  return t.printOn(os);
67}
68
69#endif
Note: See TracBrowser for help on using the browser.