1#pragma once 2#include <stdint.h> 3#include <time.h> 4#include <sys/time.h> 5 6inline double now() 7{ 8 struct timeval tv; 9 gettimeofday(&tv, NULL); 10 return tv.tv_sec + tv.tv_usec / 1e6; 11} 12 13 14struct Timer 15{ 16 Timer() 17 : start_(0), total_(0) 18 { 19 } 20 21 void start() 22 { 23 start_ = gettime(); 24 } 25 26 void stop() 27 { 28 total_ += gettime() - start_; 29 } 30 31 void reset() 32 { 33 start_ = 0; 34 total_ = 0; 35 } 36 37 double seconds() const 38 { 39 return total_ / 1e9; 40 } 41 42 static int64_t gettime() 43 { 44 struct timespec ts; 45 clock_gettime(CLOCK_MONOTONIC_RAW, &ts); 46 return ts.tv_sec * 1e9 + ts.tv_nsec; 47 } 48 49 private: 50 int64_t start_, total_; 51}; 52