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#ifndef _WIN32
6#include <sys/resource.h>
7#include <sys/time.h>
8#else
9// clang-format off
10#include <windows.h>
11#include <psapi.h>
12// clang-format on
13#endif
14
19class Timer {
20 private:
21 struct timespec _startTime;
22 struct timespec _endTime;
23
24 static constexpr int64_t NanosecondsPerSecond = 1000LL * 1000 * 1000;
25
29 void start() { clock_gettime(CLOCK_REALTIME, &_startTime); }
30
34 void end() { clock_gettime(CLOCK_REALTIME, &_endTime); }
35
36 public:
42 : _endTime({})
43 {
44 start();
45 }
46
50 [[nodiscard]] int64_t nanoseconds() const
51 {
52 struct timespec end;
53 if (_endTime.tv_nsec == 0 && _endTime.tv_sec == 0) {
54 clock_gettime(CLOCK_REALTIME, &end);
55 } else {
56 end = _endTime;
57 }
58
59 int64_t nanos = (end.tv_sec - _startTime.tv_sec) * NanosecondsPerSecond;
60 nanos += (end.tv_nsec - _startTime.tv_nsec);
61
62 return nanos;
63 }
64
68 [[nodiscard]] int64_t milliseconds() const
69 {
70 int64_t nanos = nanoseconds();
71 return nanos / 1000000;
72 }
73
77 [[nodiscard]] double seconds() const
78 {
79 int64_t nanos = nanoseconds();
80 double secs = static_cast<double>(nanos) / NanosecondsPerSecond;
81 return secs;
82 }
83
87 [[nodiscard]] std::string toString() const
88 {
89 double secs = seconds();
90 return std::to_string(secs);
91 }
92};
Get the execution between a block of code.
Definition timer.hpp:19
struct timespec _startTime
Definition timer.hpp:21
void start()
Manually sets the start time.
Definition timer.hpp:29
static constexpr int64_t NanosecondsPerSecond
Definition timer.hpp:24
struct timespec _endTime
Definition timer.hpp:22
int64_t nanoseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition timer.hpp:50
Timer()
Initialize a Timer with the current time.
Definition timer.hpp:41
double seconds() const
Return the number of seconds elapsed since the start of the timer.
Definition timer.hpp:77
std::string toString() const
Return the number of seconds elapsed since the start of the timer as a string.
Definition timer.hpp:87
void end()
Manually sets the end time.
Definition timer.hpp:34
int64_t milliseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition timer.hpp:68
std::string to_string(bb::avm2::ValueTag tag)