refactor: return numerical value from timer

This commit is contained in:
Michael Mandl 2024-03-20 09:58:40 +01:00
parent f7634bdd95
commit 64ae4c62a9
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7
3 changed files with 17 additions and 16 deletions

View file

@ -1,16 +1,17 @@
#pragma once
#include <chrono>
#include <string>
class Timer {
private:
std::string name_;
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
std::chrono::time_point<std::chrono::high_resolution_clock> end_;
public:
Timer(std::string_view name);
Timer();
void start();
void stop();
long us() const;
};

View file

@ -1,17 +1,14 @@
#include "timer.h"
#include <chrono>
#include <iostream>
Timer::Timer(std::string_view name) : name_(name) { start(); };
Timer::Timer() { start(); };
void Timer::start() { start_ = std::chrono::high_resolution_clock::now(); }
void Timer::stop() {
auto end = std::chrono::high_resolution_clock::now();
void Timer::stop() { end_ = std::chrono::high_resolution_clock::now(); }
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start_);
std::cout << name_ << " took " << duration << std::endl;
long Timer::us() const {
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_)
.count();
}