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 #include <sstream> 21 22 #if __cplusplus > 201103L && !defined(__WIN32) // C++14 23 using namespace std::chrono_literals; 24 #endif 25 26 namespace android { 27 namespace base { 28 29 // A std::chrono clock based on CLOCK_BOOTTIME. 30 class boot_clock { 31 public: 32 typedef std::chrono::nanoseconds duration; 33 typedef std::chrono::time_point<boot_clock, duration> time_point; 34 35 static time_point now(); 36 }; 37 38 class Timer { 39 public: Timer()40 Timer() : start_(boot_clock::now()) {} 41 duration()42 std::chrono::milliseconds duration() const { 43 return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_); 44 } 45 46 private: 47 boot_clock::time_point start_; 48 }; 49 50 std::ostream& operator<<(std::ostream& os, const Timer& t); 51 52 } // namespace base 53 } // namespace android 54