blob: d03c0cc752fdbcf643949b026d48a5087a474f5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#pragma once
#include <iostream>
#include <sstream>
#include <chrono>
#include <ctime>
namespace marian {
namespace timer {
// Helper function to get the current date and time
static inline std::string currentDate() {
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char date[100] = {0};
std::strftime(date, sizeof(date), "%F %X %z", std::localtime(&now));
return date;
}
// Timer measures elapsed time.
class Timer {
protected:
using clock = std::chrono::steady_clock;
using time_point = std::chrono::time_point<clock>;
using duration = std::chrono::nanoseconds;
time_point start_; // Starting time point
bool stopped_{false}; // Indicator if the timer has been stopped
duration time_; // Time duration from start() to stop()
public:
// Create and start the timer
Timer() : start_(clock::now()) {}
// Restart the timer. It is not resuming
void start() {
stopped_ = false;
start_ = clock::now();
}
// Stop the timer
void stop() {
if(stopped_)
return;
stopped_ = true;
time_ = clock::now() - start_;
}
// Check if the timer has been stopped
bool stopped() const { return stopped_; }
// Get the time elapsed without stopping the timer.
// If the template type is not specified, it returns the time counts as represented by
// std::chrono::seconds
template <class Duration = std::chrono::seconds>
double elapsed() const {
using duration_double = std::chrono::duration<double, typename Duration::period>;
if(stopped_)
return std::chrono::duration_cast<duration_double>(time_).count();
return std::chrono::duration_cast<duration_double>(clock::now() - start_).count();
}
// Default desctructor
virtual ~Timer() {}
};
// Automatic timer displays timing information on the standard output stream when it is destroyed
class AutoTimer : public Timer {
public:
// Create and start the timer
AutoTimer() : Timer() {}
// Stop the timer and display time elapsed on std::cout
~AutoTimer() {
stop();
std::cout << "Time: " << elapsed() << "s wall" << std::endl;
}
};
// std::chrono::steady_clock seems to be the right choice here.
using CPUTimer = Timer;
} // namespace timer
} // namespace marian
|