1 /* 2 * Copyright 2022 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 <bluetooth/log.h> 20 21 #include <chrono> 22 #include <cstdint> 23 #include <string> 24 25 class Stopwatch { 26 public: Stopwatch(std::string name)27 Stopwatch(std::string name) 28 : name_(std::move(name)), 29 start_(std::chrono::duration_cast<std::chrono::milliseconds>( 30 std::chrono::system_clock::now().time_since_epoch()) 31 .count()) {} 32 LapMs()33 uint64_t LapMs() const { 34 uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>( 35 std::chrono::system_clock::now().time_since_epoch()) 36 .count(); 37 return now - start_; 38 } 39 ToString()40 std::string ToString() { return ToString(""); } 41 ToString(const std::string & comment)42 std::string ToString(const std::string& comment) { 43 return fmt::format("{}: {} ms {}", name_, 44 static_cast<unsigned long>(LapMs()), comment); 45 } 46 47 private: 48 std::string name_; 49 uint64_t start_; 50 }; 51