1 // Copyright Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #ifndef S2_BASE_TIMER_H_
17 #define S2_BASE_TIMER_H_
18 
19 #include <chrono>
20 
21 #include "s2/base/integral_types.h"
22 
23 class CycleTimer {
24  public:
25   CycleTimer() = default;
26 
Start()27   void Start() {
28     start_ = Now();
29   }
30 
GetInMs()31   int64 GetInMs() const {
32     using msec = std::chrono::milliseconds;
33     return std::chrono::duration_cast<msec>(GetDuration()).count();
34   }
35 
36  private:
37   using Clock = std::chrono::high_resolution_clock;
38 
Now()39   static Clock::time_point Now() {
40     return Clock::now();
41   }
42 
GetDuration()43   Clock::duration GetDuration() const {
44     return Now() - start_;
45   }
46 
47   Clock::time_point start_;
48 };
49 
50 #endif  // S2_BASE_TIMER_H_
51