MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
timer.h
1// @file timer.h
2
3/* Copyright 2010 10gen Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include "mongo/client/export_macros.h"
21
22namespace mongo {
23
35class Timer /*copyable*/ {
36public:
37 static const long long millisPerSecond = 1000;
38 static const long long microsPerSecond = 1000 * millisPerSecond;
39 static const long long nanosPerSecond = 1000 * microsPerSecond;
40
41 Timer() {
42 reset();
43 }
44 int seconds() const {
45 return (int)(micros() / 1000000);
46 }
47 int millis() const {
48 return (int)(micros() / 1000);
49 }
50 int minutes() const {
51 return seconds() / 60;
52 }
53
54
58 inline int millisReset() {
59 const long long nextNow = now();
60 const long long deltaMicros = static_cast<long long>((nextNow - _old) * _microsPerCount);
61
62 _old = nextNow;
63 return static_cast<int>(deltaMicros / 1000);
64 }
65
66 inline long long micros() const {
67 return static_cast<long long>((now() - _old) * _microsPerCount);
68 }
69
70 inline void reset() {
71 _old = now();
72 }
73
74 inline static void setCountsPerSecond(long long countsPerSecond) {
75 _countsPerSecond = countsPerSecond;
76 _microsPerCount = static_cast<double>(microsPerSecond) / _countsPerSecond;
77 }
78
79 inline static long long getCountsPerSecond() {
80 return _countsPerSecond;
81 }
82
83private:
89 static long long _countsPerSecond;
90
91 // Derived value from _countsPerSecond. This represents the conversion ratio
92 // from clock ticks to microseconds.
93 static double _microsPerCount;
94
95 long long now() const;
96
97 long long _old;
98};
99} // namespace mongo
Time tracking object.
Definition timer.h:35
int millisReset()
Get the time interval and reset at the same time.
Definition timer.h:58
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20