STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#ifndef _TIMER_HPP_
2#define _TIMER_HPP_
3#include <time.h>
4
5#include <cstdint>
6
7class Timer {
8public:
9 // Starts the timer
10 /*
11 * @return: A timespec struct with the start time
12 */
13 static inline struct timespec tic() {
14 struct timespec startTime;
15 clock_gettime(CLOCK_MONOTONIC, &startTime);
16 return startTime;
17 }
18
19 // Stops the timer and returns the elapsed time in seconds.
20 /*
21 * @param startTime: The timespec struct with the start time of the timer
22 * @return: The elapsed time in seconds as a double
23 */
24 static inline double toc(const struct timespec& startTime) {
25 struct timespec endTime;
26 clock_gettime(CLOCK_MONOTONIC, &endTime);
27 return (static_cast<double>(endTime.tv_sec) - static_cast<double>(startTime.tv_sec)) +
28 (static_cast<double>(endTime.tv_nsec) - static_cast<double>(startTime.tv_nsec)) /
29 1e9;
30 }
31
32 // Get current time in seconds as a double
33 /*
34 * @param time_s: (output) Sets time_s to the current time in seconds as a
35 * double
36 */
37 static inline void get_time(double& time_s) {
38 struct timespec curTime;
39 clock_gettime(CLOCK_MONOTONIC, &curTime);
40 time_s = curTime.tv_sec + curTime.tv_nsec / 1e9;
41 }
42
43 // Get current time in seconds as a double
44 /*
45 * @return: The current time in seconds as a double
46 */
47 static inline double get_time() {
48 double time_s;
49 get_time(time_s);
50 return time_s;
51 }
52
53 // Get current time in nanoseconds as an int64_t
54 /*
55 * @param time_ns: (output) Sets time_ns to the current time in nanoseconds
56 * as an int64_t
57 */
58 static inline void get_time(uint64_t& time_ns) {
59 struct timespec curTime;
60 clock_gettime(CLOCK_MONOTONIC, &curTime);
61 time_ns = static_cast<uint64_t>(curTime.tv_sec) * static_cast<int64_t>(1000000000) +
62 curTime.tv_nsec;
63 }
64
65 // Get current time in nanoseconds as an int64_t
66 /*
67 * @return: The current time in nanoseconds as an int64_t
68 */
69 static inline uint64_t get_time_ns() {
70 uint64_t time_ns;
71 get_time(time_ns);
72 return time_ns;
73 }
74
75 // Get current time in seconds as two int32_ts, seconds and nanoseconds
76 /*
77 * @param time_sec: (output) Sets time_sec to the seconds part of the
78 * current time
79 * @param time_nsec: (output) Sets time_nsec to the nanoseconds part of the
80 * current time
81 */
82 static inline void get_time(int32_t& time_sec, int32_t& time_nsec) {
83 struct timespec curTime;
84 clock_gettime(CLOCK_MONOTONIC, &curTime);
85 time_sec = static_cast<int32_t>(curTime.tv_sec);
86 time_nsec = static_cast<int32_t>(curTime.tv_nsec);
87 }
88};
89#endif // _TIMER_HPP_
Definition Timer.hpp:7
static void get_time(uint64_t &time_ns)
Definition Timer.hpp:58
static uint64_t get_time_ns()
Definition Timer.hpp:69
static void get_time(double &time_s)
Definition Timer.hpp:37
static void get_time(int32_t &time_sec, int32_t &time_nsec)
Definition Timer.hpp:82
static double toc(const struct timespec &startTime)
Definition Timer.hpp:24
static struct timespec tic()
Definition Timer.hpp:13
static double get_time()
Definition Timer.hpp:47