1 // http://stackoverflow.com/questions/17432502/how-can-i-measure-cpu-time-and-wall-clock-time-on-both-linux-windows 2 3 // Windows 4 #ifdef _WIN32 5 #include <Windows.h> 6 get_wall_time()7inline double get_wall_time() 8 { 9 LARGE_INTEGER time, freq; 10 if (!QueryPerformanceFrequency(&freq)) 11 { 12 // Handle error 13 return 0; 14 } 15 if (!QueryPerformanceCounter(&time)) 16 { 17 // Handle error 18 return 0; 19 } 20 return (double) time.QuadPart / freq.QuadPart; 21 } 22 get_cpu_time()23inline double get_cpu_time() 24 { 25 FILETIME a, b, c, d; 26 if (GetProcessTimes(GetCurrentProcess(), &a, &b, &c, &d) != 0) 27 { 28 // Returns total user time. 29 // Can be tweaked to include kernel times as well. 30 return (double) (d.dwLowDateTime | 31 ((unsigned long long) d.dwHighDateTime << 32)) * 32 0.0000001; 33 } 34 else 35 { 36 // Handle error 37 return 0; 38 } 39 } 40 41 // Posix/Linux 42 #else 43 #include <time.h> 44 #include <sys/time.h> 45 get_wall_time()46inline double get_wall_time() 47 { 48 struct timeval time; 49 if (gettimeofday(&time, NULL)) 50 { 51 // Handle error 52 return 0; 53 } 54 return (double) time.tv_sec + (double) time.tv_usec * .000001; 55 } 56 get_cpu_time()57inline double get_cpu_time() 58 { 59 return (double) clock() / CLOCKS_PER_SEC; 60 } 61 62 #endif 63