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 #include "chrome/browser/sync/util/highres_timer.h" |
| 8 |
| 9 bool HighresTimer::perf_freq_collected_ = false; |
| 10 ULONGLONG HighresTimer::perf_freq_ = 0; |
| 11 |
| 12 ULONGLONG HighresTimer::GetElapsedMs() const { |
| 13 ULONGLONG end_time = GetCurrentTicks(); |
| 14 |
| 15 // Scale to ms and round to nearest ms - rounding is important because |
| 16 // otherwise the truncation error may accumulate e.g. in sums. |
| 17 // |
| 18 // Given infinite resolution, this expression could be written as: |
| 19 // trunc((end - start (units:freq*sec))/freq (units:sec) * |
| 20 // 1000 (unit:ms) + 1/2 (unit:ms)) |
| 21 ULONGLONG freq = GetTimerFrequency(); |
| 22 return ((end_time - start_ticks_) * 1000L + freq / 2) / freq; |
| 23 } |
| 24 |
| 25 ULONGLONG HighresTimer::GetElapsedSec() const { |
| 26 ULONGLONG end_time = GetCurrentTicks(); |
| 27 |
| 28 // Round to nearest ms - rounding is important because otherwise the |
| 29 // truncation error may accumulate e.g. in sums. |
| 30 // |
| 31 // Given infinite resolution, this expression could be written as: |
| 32 // trunc((end - start (units:freq*sec))/freq (unit:sec) + 1/2 (unit:sec)) |
| 33 ULONGLONG freq = GetTimerFrequency(); |
| 34 return ((end_time - start_ticks_) + freq / 2) / freq; |
| 35 } |
| 36 |
| 37 void HighresTimer::CollectPerfFreq() { |
| 38 LARGE_INTEGER freq; |
| 39 |
| 40 // Note that this is racy. It's OK, however, because even concurrent |
| 41 // executions of this are idempotent. |
| 42 if (::QueryPerformanceFrequency(&freq)) { |
| 43 perf_freq_ = freq.QuadPart; |
| 44 perf_freq_collected_ = true; |
| 45 } |
| 46 } |
OLD | NEW |