windows porting first phase- ported os layer, storage module to windows
[csql.git] / include / NanoTimer.h
blob1cebec796ab1b1670d465632c8553eca4a8bd1da
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<time.h>
10 #include<limits.h>
11 class DllExport NanoTimer
13 long long max_, min_, total, count, last_;
14 #ifdef WINNT
15 unsigned __int64 begin, end;
16 #else
17 struct timespec begin;
18 struct timespec end;
19 #endif
20 public:
21 NanoTimer() { reset(); }
22 void reset() { max_ = 0; min_ = LONG_MAX; total =0; count = 0; last_ =0; }
23 void start()
25 count++;
26 #ifdef WINNT
27 FILETIME ft;
28 begin = 0;
29 GetSystemTimeAsFileTime(&ft);
30 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
31 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
32 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
33 begin |= ft.dwHighDateTime;
34 begin <<= 32;
35 begin |= ft.dwLowDateTime;
36 #else
37 clock_gettime(CLOCK_REALTIME, &begin);
38 #endif
40 void stop()
42 #ifdef WINNT
43 FILETIME ft;
44 end = 0;
45 GetSystemTimeAsFileTime(&ft);
46 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
47 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
48 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
49 end |= ft.dwHighDateTime;
50 end <<= 32;
51 end |= ft.dwLowDateTime;
52 last_ = end - begin;
53 #else
54 clock_gettime(CLOCK_REALTIME, &end);
55 long long secs = end.tv_sec-begin.tv_sec;
56 long long nano = end.tv_nsec-begin.tv_nsec;
57 last_ = (secs*1000000000)+nano;
58 #endif
59 total += last_;
60 if ( max_ < last_ ) max_ = last_;
61 if ( min_ > last_ ) min_ = last_;
63 long long last() { return last_; }
64 long long avg() { return total/count; }
65 long long sum() { return total; }
66 long long minc() { return min_; }
67 long long maxc() { return max_; }
70 #endif