Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1#pragma once
2#include <cstdio>
3#include <ctime>
4#include <string>
5#include <sys/resource.h>
6#include <sys/time.h>
7
12class Timer {
13 private:
14 struct timespec _startTime;
15 struct timespec _endTime;
16
17 static constexpr int64_t NanosecondsPerSecond = 1000LL * 1000 * 1000;
18
22 void start() { clock_gettime(CLOCK_REALTIME, &_startTime); }
23
27 void end() { clock_gettime(CLOCK_REALTIME, &_endTime); }
28
29 public:
35 : _endTime({})
36 {
37 start();
38 }
39
43 [[nodiscard]] int64_t nanoseconds() const
44 {
45 struct timespec end;
46 if (_endTime.tv_nsec == 0 && _endTime.tv_sec == 0) {
47 clock_gettime(CLOCK_REALTIME, &end);
48 } else {
49 end = _endTime;
50 }
51
52 int64_t nanos = (end.tv_sec - _startTime.tv_sec) * NanosecondsPerSecond;
53 nanos += (end.tv_nsec - _startTime.tv_nsec);
54
55 return nanos;
56 }
57
61 [[nodiscard]] int64_t milliseconds() const
62 {
63 int64_t nanos = nanoseconds();
64 return nanos / 1000000;
65 }
66
70 [[nodiscard]] double seconds() const
71 {
72 int64_t nanos = nanoseconds();
73 double secs = static_cast<double>(nanos) / NanosecondsPerSecond;
74 return secs;
75 }
76
80 [[nodiscard]] std::string toString() const
81 {
82 double secs = seconds();
83 return std::to_string(secs);
84 }
85};
Get the execution between a block of code.
Definition timer.hpp:12
struct timespec _startTime
Definition timer.hpp:14
void start()
Manually sets the start time.
Definition timer.hpp:22
static constexpr int64_t NanosecondsPerSecond
Definition timer.hpp:17
struct timespec _endTime
Definition timer.hpp:15
int64_t nanoseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition timer.hpp:43
Timer()
Initialize a Timer with the current time.
Definition timer.hpp:34
double seconds() const
Return the number of seconds elapsed since the start of the timer.
Definition timer.hpp:70
std::string toString() const
Return the number of seconds elapsed since the start of the timer as a string.
Definition timer.hpp:80
void end()
Manually sets the end time.
Definition timer.hpp:27
int64_t milliseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition timer.hpp:61
std::string to_string(bb::avm2::ValueTag tag)