MolSim
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1
9#pragma once
10#include "CLIUtils.h"
11#include <chrono>
12#include <iostream>
13#include <spdlog/spdlog.h>
14#include <string>
15
16#ifdef DO_BENCHMARKING
17#define TIMER_RESET(_x) _x.reset()
18#define TIMER_PRINT_ELAPSED(_x) _x.printElapsed()
19#define TIMER_PRINT_MUPS(_x) _x.printMUPS()
20#define TIMER_UPDATE_MOLECULES(_x, _y) _x.incrementMoleculeUpdates(_y)
21#else
22#define TIMER_RESET(_x) (void)0
23#define TIMER_PRINT_ELAPSED(_x) (void)0
24#define TIMER_PRINT_MUPS(_x) (void)0
25#define TIMER_UPDATE_MOLECULES(_x, _y) (void)0
26#endif
27
29class Timer {
30 private:
32 std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
33
36
37 public:
40 SPDLOG_TRACE("Created a new blank timer... you shouldn't be seeing this if you want to measure runtime!");
41 }
42
44 inline void reset() { m_start = std::chrono::high_resolution_clock::now(); }
45
52 template <typename T> inline auto elapsed() const {
53 return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - m_start).count();
54 }
55
61 inline void incrementMoleculeUpdates(long summand) { m_moleculeUpdates += summand; }
62
64 inline void printElapsed() const {
65 auto elapsedInMillis = elapsed<std::chrono::milliseconds>();
66 auto elapsedInSecs = elapsedInMillis * 0.001;
67 SPDLOG_INFO("Elapsed: {}ms ({}s)", elapsedInMillis, elapsedInSecs);
68 }
69
71 inline void printMUPS() const {
72 auto elapsedInSec = (elapsed<std::chrono::milliseconds>() * 0.001);
73 SPDLOG_INFO("Molecule Updates per Second (MUPS): {}", static_cast<double>(m_moleculeUpdates) / elapsedInSec);
74 }
75};
Utility functions for console input / output.
Class used to measure runtime performance and log molecule updates per second (MUPS).
Definition Timer.h:29
Timer()
Constructs a new Timer.
Definition Timer.h:39
void printElapsed() const
Prints the elapsed time.
Definition Timer.h:64
void incrementMoleculeUpdates(long summand)
Increments the total number of molecule updates by a given amount.
Definition Timer.h:61
void reset()
Resets the start time.
Definition Timer.h:44
auto elapsed() const
Gets the elapsed time since the start.
Definition Timer.h:52
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
The start time of the timing process.
Definition Timer.h:32
long m_moleculeUpdates
The total number of molecule updates in the given timeframe.
Definition Timer.h:35
void printMUPS() const
Prints the molecule updates per second.
Definition Timer.h:71