MongoDB C++ Driver  legacy-1.0.3
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
22 namespace mongo {
23 
35  class Timer /*copyable*/ {
36  public:
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() { reset(); }
42  int seconds() const { return (int)(micros() / 1000000); }
43  int millis() const { return (int)(micros() / 1000); }
44  int minutes() const { return seconds() / 60; }
45 
46 
50  inline int millisReset() {
51  const long long nextNow = now();
52  const long long deltaMicros =
53  static_cast<long long>((nextNow - _old) * _microsPerCount);
54 
55  _old = nextNow;
56  return static_cast<int>(deltaMicros / 1000);
57  }
58 
59  inline long long micros() const {
60  return static_cast<long long>((now() - _old) * _microsPerCount);
61  }
62 
63  inline void reset() { _old = now(); }
64 
65  inline static void setCountsPerSecond(long long countsPerSecond) {
66  _countsPerSecond = countsPerSecond;
67  _microsPerCount = static_cast<double>(microsPerSecond) / _countsPerSecond;
68  }
69 
70  inline static long long getCountsPerSecond() {
71  return _countsPerSecond;
72  }
73 
74  private:
80  static long long _countsPerSecond;
81 
82  // Derived value from _countsPerSecond. This represents the conversion ratio
83  // from clock ticks to microseconds.
84  static double _microsPerCount;
85 
86  long long now() const;
87 
88  long long _old;
89  };
90 } // namespace mongo
int millisReset()
Get the time interval and reset at the same time.
Definition: timer.h:50
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
Time tracking object.
Definition: timer.h:35