blob: fab657a8c36cbbb0f871e9bc96263d00f77fa33f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.combe9ad4e2011-06-07 19:16:02 +00008#include "BenchSysTimer_windows.h"
9
10//Time
11#define WIN32_LEAN_AND_MEAN 1
bungeman@google.com0d9e3da2013-12-03 15:23:37 +000012#include <windows.h>
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000013
14static 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
34void BenchSysTimer::startWall() {
35 if (0 == ::QueryPerformanceCounter(&this->fStartWall)) {
36 this->fStartWall.QuadPart = 0;
37 }
38}
39void BenchSysTimer::startCpu() {
40 this->fStartCpu = winCpuTime();
41}
42
43double BenchSysTimer::endCpu() {
44 ULONGLONG end_cpu = winCpuTime();
bungeman@google.coma38c8192011-10-03 20:44:39 +000045 return static_cast<double>((end_cpu - this->fStartCpu)) / 10000.0L;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000046}
47double BenchSysTimer::endWall() {
48 LARGE_INTEGER end_wall;
49 if (0 == ::QueryPerformanceCounter(&end_wall)) {
50 end_wall.QuadPart = 0;
51 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000053 LARGE_INTEGER ticks_elapsed;
54 ticks_elapsed.QuadPart = end_wall.QuadPart - this->fStartWall.QuadPart;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000056 LARGE_INTEGER frequency;
57 if (0 == ::QueryPerformanceFrequency(&frequency)) {
bungeman@google.coma38c8192011-10-03 20:44:39 +000058 return 0.0L;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000059 } else {
bungeman@google.coma38c8192011-10-03 20:44:39 +000060 return static_cast<double>(ticks_elapsed.QuadPart)
bungeman@google.comc9614422011-10-19 22:10:21 +000061 / static_cast<double>(frequency.QuadPart)
62 * 1000.0L;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +000063 }
64}