00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef NANO_TIMER_H
00017 #define NANO_TIMER_H
00018 #include<time.h>
00019 #include<limits.h>
00020 class NanoTimer
00021 {
00022 long long max_, min_, total, count, last_;
00023 struct timespec begin;
00024 struct timespec end;
00025
00026 public:
00027 NanoTimer() { reset(); }
00028 void reset() { max_ = 0; min_ = LONG_MAX; total =0; count = 0; last_ =0; }
00029 void start()
00030 {
00031 count++;
00032 clock_gettime(CLOCK_REALTIME, &begin);
00033 }
00034 void stop()
00035 {
00036 clock_gettime(CLOCK_REALTIME, &end);
00037 long long secs = end.tv_sec-begin.tv_sec;
00038 long long nano = end.tv_nsec-begin.tv_nsec;
00039 last_ = (secs*1000000000)+nano;
00040 total += last_;
00041 if ( max_ < last_ ) max_ = last_;
00042 if ( min_ > last_ ) min_ = last_;
00043 }
00044 long long last() { return last_; }
00045 long long avg() { return total/count; }
00046 long long min() { return min_; }
00047 long long max() { return max_; }
00048 };
00049
00050 #endif