File: timer.h

package info (click to toggle)
xosview 1.16-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,100 kB
  • ctags: 1,434
  • sloc: cpp: 11,003; makefile: 167; ansic: 32; awk: 13; sh: 8
file content (52 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
//
//  This file may be distributed under terms of the GPL
//

#ifndef _TIMER_H_
#define _TIMER_H_

//
//                 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 );  }

  //  This one uses doubles as the return value, to avoid
  //  overflow/sign problems.
  double report_usecs(void) const {
    return (stoptime_.tv_sec - starttime_.tv_sec) * 1000000.0
      + stoptime_.tv_usec - starttime_.tv_usec;
  }

  std::ostream &printOn(std::ostream &os) const {
    return os <<"Timer : ["
      <<"starttime_ = " <<TimeVal(starttime_)
      <<", stoptime_ = " <<TimeVal(stoptime_)
      <<", duration = " <<report_usecs() <<" usecs]";
  }

protected:
  struct timeval starttime_, stoptime_;

private:
};

inline std::ostream &operator<<(std::ostream &os, const Timer &t){
  return t.printOn(os);
}

#endif