|
4 | 4 | * Apache License Version 2.0 http://www.apache.org/licenses/. |
5 | 5 | * (c) Daniel Lemire 2013 |
6 | 6 | */ |
7 | | - |
| 7 | +#include <chrono> |
8 | 8 | #include <sys/stat.h> |
9 | | -#include <sys/time.h> |
10 | 9 | #include <sys/types.h> |
11 | 10 | #ifdef _OPENMP |
12 | 11 | #include <omp.h> |
|
20 | 19 |
|
21 | 20 | using namespace std; |
22 | 21 |
|
| 22 | + |
| 23 | +// |
| 24 | +// VS2012 bug: high_precision_clock is defined as system_clock and precision is |
| 25 | +// about 15 MS!! |
| 26 | +// See: https://connect.microsoft.com/VisualStudio/feedback/details/719443 |
| 27 | +// |
| 28 | +// Implementation has been taken from a post on stackoverflow and adapted here |
| 29 | +// http://stackoverflow.com/questions/13263277/difference-between-stdsystem-clock-and-stdsteady-clock |
| 30 | +// |
| 31 | +#ifdef _WIN32 |
| 32 | +#define NOMINMAX |
| 33 | +#define WINDOWS_LEAN_AND_MEAN |
| 34 | +#include <windows.h> |
| 35 | + |
| 36 | +struct qpc_clock { |
| 37 | + typedef std::chrono::nanoseconds duration; |
| 38 | + typedef duration::rep rep; |
| 39 | + typedef duration::period period; |
| 40 | + typedef std::chrono::time_point<qpc_clock, duration> time_point; |
| 41 | + static time_point now() { |
| 42 | + static bool isInited = false; |
| 43 | + static LARGE_INTEGER frequency = { 0, 0 }; |
| 44 | + if (!isInited) { |
| 45 | + if (QueryPerformanceFrequency(&frequency) == 0) { |
| 46 | + throw std::logic_error("QueryPerformanceCounter not supported: " + |
| 47 | + std::to_string(GetLastError())); |
| 48 | + } |
| 49 | + isInited = true; |
| 50 | + } |
| 51 | + LARGE_INTEGER counter; |
| 52 | + QueryPerformanceCounter(&counter); |
| 53 | + return time_point(duration(static_cast<rep>((double)counter.QuadPart / |
| 54 | + frequency.QuadPart * |
| 55 | + period::den / period::num))); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +#endif |
| 60 | + |
| 61 | +/** |
| 62 | +* author: Preston Bannister |
| 63 | +*/ |
23 | 64 | class WallClockTimer { |
24 | 65 | public: |
25 | | - struct timeval t1, t2; |
| 66 | +#ifdef _WIN32 |
| 67 | + typedef qpc_clock clock; |
| 68 | +#else |
| 69 | + typedef std::chrono::high_resolution_clock clock; |
| 70 | +#endif |
26 | 71 |
|
| 72 | + std::chrono::time_point<clock> t1, t2; |
| 73 | + WallClockTimer() : t1(), t2() { |
| 74 | + t1 = clock::now(); |
| 75 | + t2 = t1; |
| 76 | + } |
| 77 | + void reset() { |
| 78 | + t1 = clock::now(); |
| 79 | + t2 = t1; |
| 80 | + } |
| 81 | + uint64_t elapsed() { |
| 82 | + std::chrono::microseconds delta = |
| 83 | + std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1); |
| 84 | + return delta.count(); |
| 85 | + } |
| 86 | + uint64_t split() { |
| 87 | + t2 = clock::now(); |
| 88 | + return elapsed(); |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +#ifndef _WIN32 |
| 93 | + |
| 94 | +class CPUTimer { |
27 | 95 | public: |
28 | | - WallClockTimer() : t1(), t2() { |
29 | | - gettimeofday(&t1, 0); |
30 | | - t2 = t1; |
31 | | - } |
32 | | - void reset() { |
33 | | - gettimeofday(&t1, 0); |
34 | | - t2 = t1; |
35 | | - } |
36 | | - int elapsed() { |
37 | | - return ((t2.tv_sec - t1.tv_sec) * 1000) + |
38 | | - ((t2.tv_usec - t1.tv_usec) / 1000); |
39 | | - } |
40 | | - int split() { |
41 | | - gettimeofday(&t2, 0); |
42 | | - return elapsed(); |
43 | | - } |
| 96 | + // clock_t t1, t2; |
| 97 | + struct rusage t1, t2; |
| 98 | + |
| 99 | + CPUTimer() : t1(), t2() { |
| 100 | + getrusage(RUSAGE_SELF, &t1); |
| 101 | + // t1 = clock(); |
| 102 | + t2 = t1; |
| 103 | + } |
| 104 | + void reset() { |
| 105 | + getrusage(RUSAGE_SELF, &t1); |
| 106 | + t2 = t1; |
| 107 | + } |
| 108 | + // proxy for userelapsed |
| 109 | + uint64_t elapsed() { return totalelapsed(); } |
| 110 | + |
| 111 | + uint64_t totalelapsed() { return userelapsed() + systemelapsed(); } |
| 112 | + // returns the *user* CPU time in micro seconds (mu s) |
| 113 | + uint64_t userelapsed() { |
| 114 | + return ((t2.ru_utime.tv_sec - t1.ru_utime.tv_sec) * 1000ULL * 1000ULL) + |
| 115 | + ((t2.ru_utime.tv_usec - t1.ru_utime.tv_usec)); |
| 116 | + } |
| 117 | + |
| 118 | + // returns the *system* CPU time in micro seconds (mu s) |
| 119 | + uint64_t systemelapsed() { |
| 120 | + return ((t2.ru_stime.tv_sec - t1.ru_stime.tv_sec) * 1000ULL * 1000ULL) + |
| 121 | + ((t2.ru_stime.tv_usec - t1.ru_stime.tv_usec)); |
| 122 | + } |
| 123 | + |
| 124 | + uint64_t split() { |
| 125 | + getrusage(RUSAGE_SELF, &t2); |
| 126 | + return elapsed(); |
| 127 | + } |
44 | 128 | }; |
45 | 129 |
|
| 130 | +#endif |
| 131 | + |
46 | 132 | void displayUsage() { cout << "run as test nameoffile" << endl; } |
47 | 133 |
|
48 | 134 | vector<uint32_t> loadVector(string filename) { |
|
0 commit comments