windows porting
[csql.git] / include / NanoTimer.h
blob22f89c4be0add96e913543a2cce08e82953eafc1
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef NANO_TIMER_H
8 #define NANO_TIMER_H
9 #include<os.h>
10 #include<time.h>
11 #include<limits.h>
12 class DllExport NanoTimer
14 long long max_, min_, total, count, last_;
15 #ifdef WINNT
16 unsigned __int64 begin, end;
17 #else
18 struct timespec begin;
19 struct timespec end;
20 #endif
21 public:
22 NanoTimer() { reset(); }
23 void reset() { max_ = 0; min_ = LONG_MAX; total =0; count = 0; last_ =0; }
24 void start()
26 count++;
27 #ifdef WINNT
28 FILETIME ft;
29 begin = 0;
30 GetSystemTimeAsFileTime(&ft);
31 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
32 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
33 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
34 begin |= ft.dwHighDateTime;
35 begin <<= 32;
36 begin |= ft.dwLowDateTime;
37 #else
38 clock_gettime(CLOCK_REALTIME, &begin);
39 #endif
41 void stop()
43 #ifdef WINNT
44 FILETIME ft;
45 end = 0;
46 GetSystemTimeAsFileTime(&ft);
47 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
48 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
49 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
50 end |= ft.dwHighDateTime;
51 end <<= 32;
52 end |= ft.dwLowDateTime;
53 last_ = end - begin;
54 #else
55 clock_gettime(CLOCK_REALTIME, &end);
56 long long secs = end.tv_sec-begin.tv_sec;
57 long long nano = end.tv_nsec-begin.tv_nsec;
58 last_ = (secs*1000000000)+nano;
59 #endif
60 total += last_;
61 if ( max_ < last_ ) max_ = last_;
62 if ( min_ > last_ ) min_ = last_;
64 long long last() { return last_; }
65 long long avg() { return total/count; }
66 long long sum() { return total; }
67 long long minc() { return min_; }
68 long long maxc() { return max_; }
71 #endif