Peano
Loading...
Searching...
No Matches
Watch.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3
4#include "Watch.h"
5
6#include <sstream>
7#include <string>
8#include <sys/time.h>
9
11 const std::string& className,
12 const std::string& operationName,
13 const bool plotResultInDestructor,
14 bool startToTickImmediately // = true
15):
16 _log(className),
17 _plotResultInDestructor(plotResultInDestructor),
18 _operationName(operationName),
19 _startClockTicks(0),
20 _startTime(),
21 _elapsedClockTicks(0),
22 _elapsedTime(),
23 _isRunning(startToTickImmediately) {
24
25 if (startToTickImmediately) {
26 start();
27 }
28}
29
30
32 if (_isRunning) {
33 stop();
34 }
35
36 if (_plotResultInDestructor) {
37 logInfo(
38 _operationName,
39 "total number of clock ticks within block (cpu-time,calendar-time): "
40 << "(" << getCPUTime() << "s"
41 << "," << getCalendarTime() << "s"
42 << ")"
43 );
44 }
45}
46
47
49 _startClockTicks = std::clock();
50 _startTime = std::chrono::steady_clock::now();
51 _isRunning = true;
52}
53
54
56 _elapsedClockTicks = std::clock() - _startClockTicks;
57 _elapsedTime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - _startTime)
58 .count();
59 _elapsedTime = _elapsedTime / 1000000000.0; // Convert to seconds
60 _isRunning = false;
61}
62
63
65 double lhs = static_cast<double>(_elapsedClockTicks);
66 double rhs = static_cast<double>(CLOCKS_PER_SEC);
67 return lhs / rhs;
68}
69
70
71std::clock_t tarch::timing::Watch::getCPUTicks() { return _elapsedClockTicks; }
72
73
74double tarch::timing::Watch::getCalendarTime() { return _elapsedTime; }
75
76
77bool tarch::timing::Watch::isOn() const { return _isRunning; }
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
Watch(const std::string &className, const std::string &operationName, bool plotResultInDestructor, bool startToTickImmediately=true)
Construct a watch.
Definition Watch.cpp:10
double getCalendarTime()
This method returns the elapsed calendar time between the start and stop command of the timer,...
Definition Watch.cpp:74
std::clock_t getCPUTicks()
Equals getCPUTime() but returns the clock ticks instead of the time in seconds.
Definition Watch.cpp:71
double getCPUTime()
Return CPU Time in Seconds.
Definition Watch.cpp:64
void stop()
Stop timer.
Definition Watch.cpp:55
bool isOn() const
This operation returns whether the watch is currently on.
Definition Watch.cpp:77
void start()
(Re)Start the Timer
Definition Watch.cpp:48
virtual ~Watch()
For standard version (): Stops the watch and plots the time spent.
Definition Watch.cpp:31
tarch::logging::Log _log("exahype2::fv")