"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/timer.h" (11 Jul 2020, 1272 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "timer.h" see the Fossies "Dox" file reference documentation.

    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 #ifndef _TIMER_H_
    8 #define _TIMER_H_
    9 
   10 //
   11 //                 General purpose interval timer class
   12 //
   13 //  Implemented using BSD derived function gettimeofday for greater resolution
   14 //
   15 //   Author : Mike Romberg
   16 
   17 #include "timeval.h"
   18 #include <sys/time.h>
   19 #include <iostream>
   20 
   21 
   22 class Timer {
   23 public:
   24   Timer( int start = 0 ) { if ( start ) Timer::start(); }
   25   ~Timer( void ){}
   26 
   27   void start( void ) { gettimeofday( &starttime_, NULL ); }
   28   void stop( void )  { gettimeofday( &stoptime_, NULL );  }
   29 
   30   //  This one uses doubles as the return value, to avoid
   31   //  overflow/sign problems.
   32   double report_usecs(void) const {
   33     return (stoptime_.tv_sec - starttime_.tv_sec) * 1000000.0
   34       + stoptime_.tv_usec - starttime_.tv_usec;
   35   }
   36 
   37   std::ostream &printOn(std::ostream &os) const {
   38     return os <<"Timer : ["
   39       <<"starttime_ = " <<TimeVal(starttime_)
   40       <<", stoptime_ = " <<TimeVal(stoptime_)
   41       <<", duration = " <<report_usecs() <<" usecs]";
   42   }
   43 
   44 protected:
   45   struct timeval starttime_, stoptime_;
   46 
   47 private:
   48 };
   49 
   50 inline std::ostream &operator<<(std::ostream &os, const Timer &t){
   51   return t.printOn(os);
   52 }
   53 
   54 #endif