epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 8 | #include "BenchSysTimer_windows.h" |
| 9 | |
| 10 | //Time |
| 11 | #define WIN32_LEAN_AND_MEAN 1 |
| 12 | #include <Windows.h> |
| 13 | |
| 14 | static ULONGLONG winCpuTime() { |
| 15 | FILETIME createTime; |
| 16 | FILETIME exitTime; |
| 17 | FILETIME usrTime; |
| 18 | FILETIME sysTime; |
| 19 | if (0 == GetProcessTimes(GetCurrentProcess() |
| 20 | , &createTime, &exitTime |
| 21 | , &sysTime, &usrTime)) |
| 22 | { |
| 23 | return 0; |
| 24 | } |
| 25 | ULARGE_INTEGER start_cpu_sys; |
| 26 | ULARGE_INTEGER start_cpu_usr; |
| 27 | start_cpu_sys.LowPart = sysTime.dwLowDateTime; |
| 28 | start_cpu_sys.HighPart = sysTime.dwHighDateTime; |
| 29 | start_cpu_usr.LowPart = usrTime.dwLowDateTime; |
| 30 | start_cpu_usr.HighPart = usrTime.dwHighDateTime; |
| 31 | return start_cpu_sys.QuadPart + start_cpu_usr.QuadPart; |
| 32 | } |
| 33 | |
| 34 | void BenchSysTimer::startWall() { |
| 35 | if (0 == ::QueryPerformanceCounter(&this->fStartWall)) { |
| 36 | this->fStartWall.QuadPart = 0; |
| 37 | } |
| 38 | } |
| 39 | void BenchSysTimer::startCpu() { |
| 40 | this->fStartCpu = winCpuTime(); |
| 41 | } |
| 42 | |
| 43 | double BenchSysTimer::endCpu() { |
| 44 | ULONGLONG end_cpu = winCpuTime(); |
bungeman@google.com | a38c819 | 2011-10-03 20:44:39 +0000 | [diff] [blame] | 45 | return static_cast<double>((end_cpu - this->fStartCpu)) / 10000.0L; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 46 | } |
| 47 | double BenchSysTimer::endWall() { |
| 48 | LARGE_INTEGER end_wall; |
| 49 | if (0 == ::QueryPerformanceCounter(&end_wall)) { |
| 50 | end_wall.QuadPart = 0; |
| 51 | } |
| 52 | |
| 53 | LARGE_INTEGER ticks_elapsed; |
| 54 | ticks_elapsed.QuadPart = end_wall.QuadPart - this->fStartWall.QuadPart; |
| 55 | |
| 56 | LARGE_INTEGER frequency; |
| 57 | if (0 == ::QueryPerformanceFrequency(&frequency)) { |
bungeman@google.com | a38c819 | 2011-10-03 20:44:39 +0000 | [diff] [blame] | 58 | return 0.0L; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 59 | } else { |
bungeman@google.com | a38c819 | 2011-10-03 20:44:39 +0000 | [diff] [blame] | 60 | return static_cast<double>(ticks_elapsed.QuadPart) |
bungeman@google.com | c961442 | 2011-10-19 22:10:21 +0000 | [diff] [blame] | 61 | / static_cast<double>(frequency.QuadPart) |
| 62 | * 1000.0L; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 63 | } |
| 64 | } |