1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <chrono> 20 21 namespace slicer { 22 23 // A very simple timing chronometer 24 class Chronometer { 25 using Clock = std::chrono::high_resolution_clock; 26 27 public: 28 // elapsed time is in milliseconds 29 explicit Chronometer(double& elapsed, bool cumulative = false) : elapsed_(elapsed)30 elapsed_(elapsed), cumulative_(cumulative) { 31 start_time_ = Clock::now(); 32 } 33 ~Chronometer()34 ~Chronometer() { 35 Clock::time_point end_time = Clock::now(); 36 std::chrono::duration<double, std::milli> ms = end_time - start_time_; 37 if (cumulative_) { 38 elapsed_ += ms.count(); 39 } else { 40 elapsed_ = ms.count(); 41 } 42 } 43 44 Chronometer(const Chronometer&) = delete; 45 Chronometer& operator=(const Chronometer&) = delete; 46 47 private: 48 double& elapsed_; 49 Clock::time_point start_time_; 50 bool cumulative_; 51 }; 52 53 } // namespace slicer 54 55