AutoPas  3.0.0
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <chrono>
11#include <iomanip>
12#include <sstream>
13#include <string>
14
15namespace autopas::utils {
16
21using bestSteadyClock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
22 std::chrono::high_resolution_clock, std::chrono::steady_clock>;
23
27class Timer {
28 public:
29 Timer();
30
31 virtual ~Timer();
32
36 void start();
37
43 long stop();
44
48 void reset();
49
54 void addTime(long nanoseconds);
55
60 [[nodiscard]] long getTotalTime() const { return _totalTime; }
61
67 static std::string getDateStamp(const std::string &format = "%Y-%m-%d_%H-%M-%S") {
68 auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
69 std::ostringstream nowStrStr;
70 tm unused;
71 std::stringstream ss;
72 ss << std::put_time(localtime_r(&now, &unused), format.c_str());
73 return ss.str();
74 }
75
76 private:
80 bestSteadyClock::time_point _startTime;
81
85 long _totalTime = 0;
86
90 bool _currentlyRunning = false;
91};
92} // namespace autopas::utils
Timer class to stop times.
Definition: Timer.h:27
void start()
start the timer.
Definition: Timer.cpp:17
void reset()
Resets the timer to 0.
Definition: Timer.cpp:40
long getTotalTime() const
Get total accumulated time.
Definition: Timer.h:60
static std::string getDateStamp(const std::string &format="%Y-%m-%d_%H-%M-%S")
Create a date stamp for the current moment with the given format.
Definition: Timer.h:67
void addTime(long nanoseconds)
Adds the given amount of nanoseconds to the total time.
Definition: Timer.cpp:42
long stop()
Stops the timer and returns the time elapsed in nanoseconds since the last call to start.
Definition: Timer.cpp:25
In this namespace some helper classes and functions can be found used inside of AutoPas.
Definition: namespaces.h:44
std::conditional_t< std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock > bestSteadyClock
high_resolution_clock is not guaranteed to be monotonous in time, which can lead to negative duration...
Definition: Timer.h:22