OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // High resolution timer functions for use in Windows. |
| 6 |
| 7 #ifndef CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_WIN32_H_ |
| 8 #define CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_WIN32_H_ |
| 9 |
| 10 #include <windows.h> |
| 11 |
| 12 // A handy class for reliably measuring wall-clock time with decent resolution, |
| 13 // even on multi-processor machines and on laptops (where RDTSC potentially |
| 14 // returns different results on different processors and/or the RDTSC timer |
| 15 // clocks at different rates depending on the power state of the CPU, |
| 16 // respectively). |
| 17 class HighresTimer { |
| 18 public: |
| 19 // Captures the current start time. |
| 20 HighresTimer(); |
| 21 |
| 22 // Captures the current tick, can be used to reset a timer for reuse. |
| 23 void Start(); |
| 24 |
| 25 // Returns the elapsed ticks with full resolution. |
| 26 ULONGLONG GetElapsedTicks() const; |
| 27 |
| 28 // Returns the elapsed time in milliseconds, rounded to the nearest |
| 29 // millisecond. |
| 30 ULONGLONG GetElapsedMs() const; |
| 31 |
| 32 // Returns the elapsed time in seconds, rounded to the nearest second. |
| 33 ULONGLONG GetElapsedSec() const; |
| 34 |
| 35 ULONGLONG start_ticks() const { return start_ticks_; } |
| 36 |
| 37 // Returns timer frequency from cache, should be less |
| 38 // overhead than ::QueryPerformanceFrequency. |
| 39 static ULONGLONG GetTimerFrequency(); |
| 40 // Returns current ticks. |
| 41 static ULONGLONG GetCurrentTicks(); |
| 42 |
| 43 private: |
| 44 static void CollectPerfFreq(); |
| 45 |
| 46 // Captured start time. |
| 47 ULONGLONG start_ticks_; |
| 48 |
| 49 // Captured performance counter frequency. |
| 50 static bool perf_freq_collected_; |
| 51 static ULONGLONG perf_freq_; |
| 52 }; |
| 53 |
| 54 inline HighresTimer::HighresTimer() { |
| 55 Start(); |
| 56 } |
| 57 |
| 58 inline void HighresTimer::Start() { |
| 59 start_ticks_ = GetCurrentTicks(); |
| 60 } |
| 61 |
| 62 inline ULONGLONG HighresTimer::GetTimerFrequency() { |
| 63 if (!perf_freq_collected_) |
| 64 CollectPerfFreq(); |
| 65 return perf_freq_; |
| 66 } |
| 67 |
| 68 inline ULONGLONG HighresTimer::GetCurrentTicks() { |
| 69 LARGE_INTEGER ticks; |
| 70 ::QueryPerformanceCounter(&ticks); |
| 71 return ticks.QuadPart; |
| 72 } |
| 73 |
| 74 inline ULONGLONG HighresTimer::GetElapsedTicks() const { |
| 75 return start_ticks_ - GetCurrentTicks(); |
| 76 } |
| 77 |
| 78 #endif // CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_WIN32_H_ |
OLD | NEW |