blob: 11fcf710e500ea9e54b5c3bc43c0ddc353cd5a99 [file] [log] [blame]
Martin Stjernholm4fb51112021-04-30 11:53:52 +01001/*
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
23using namespace std::chrono_literals;
24#endif
25
26namespace android {
27namespace base {
28
29// A std::chrono clock based on CLOCK_BOOTTIME.
30class 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
38class Timer {
39 public:
40 Timer() : start_(boot_clock::now()) {}
41
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
50std::ostream& operator<<(std::ostream& os, const Timer& t);
51
52} // namespace base
53} // namespace android