JASSv2
timer.h
Go to the documentation of this file.
1 /*
2  TIMER.H
3  -------
4  Copyright (c) 2017 Andrew Trotman
5  Released under the 2-clause BSD license (See:https://en.wikipedia.org/wiki/BSD_licenses)
6 */
13 #pragma once
14 
15 #include <stdio.h>
16 #include <stdint.h>
17 
18 #include <chrono>
19 #include <thread>
20 
21 #include "asserts.h"
22 
23 namespace JASS
24  {
25  /*
26  CLASS TIMER
27  -----------
28  */
32  class timer
33  {
34  private:
35  /*
36  TYPEDEF TIMER::STOP_WATCH
37  -------------------------
38  */
43  typedef std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> stop_watch;
44 
45  /*
46  CLASS TIMER::DURATION
47  ----------------------
48  */
52  class duration
53  {
54  private:
55  std::chrono::nanoseconds took;
56 
57  public:
58  /*
59  TIMER::DURATION::DURATION()
60  ---------------------------
61  */
65  duration(std::chrono::nanoseconds value)
66  {
67  took = value;
68  }
69 
70  /*
71  TIMER::DURATION::MILLISECONDS()
72  -------------------------------
73  */
78  auto milliseconds(void)
79  {
80  return std::chrono::duration_cast<std::chrono::milliseconds>(took).count();
81  }
82 
83  /*
84  TIMER::DURATION::MICROSECONDS()
85  -------------------------------
86  */
91  auto microseconds(void)
92  {
93  return std::chrono::duration_cast<std::chrono::microseconds>(took).count();
94  }
95 
96  /*
97  TIMER::DURATION::NANOSECONDS()
98  ------------------------------
99  */
104  auto nanoseconds(void)
105  {
106  return std::chrono::duration_cast<std::chrono::nanoseconds>(took).count();
107  }
108  };
109 
110  public:
111  /*
112  TIMER::START()
113  --------------
114  */
119  static stop_watch start()
120  {
121  return std::chrono::steady_clock::now();
122  }
123 
124  /*
125  TIMER::STOP()
126  -------------
127  */
133  static duration stop(stop_watch watch)
134  {
135  return std::chrono::steady_clock::now() - watch;
136  }
137 
138  /*
139  TIMER::UNITTEST()
140  -----------------
141  */
145  static void unittest(void)
146  {
147  /*
148  Time about 100 milliseconds
149  */
150  auto clock = timer::start();
151  std::this_thread::sleep_for (std::chrono::milliseconds(100));
152  auto took = timer::stop(clock);
153 
154  /*
155  Get the actual time in different units
156  */
157  size_t nano = took.nanoseconds();
158  size_t micro = took.microseconds();
159  size_t milli = took.milliseconds();
160 
161  /*
162  Check that the timer took about the same time in nanoseconds as microseconds
163  */
164  size_t nano_as_micro = nano / 1000;
165  JASS_assert((nano_as_micro >= micro - 1) && (nano_as_micro - 1 <= micro));
166 
167  /*
168  Check that the timer took about the same time in nanoseconds as milliseconds
169  */
170  size_t nano_as_milli = nano / 1000'000;
171  JASS_assert((nano_as_milli >= milli - 1) && (nano_as_milli - 1 <= milli));
172 
173  /*
174  Yay!
175  */
176  puts("timer::PASSED");
177  }
178 
179  };
180  }
std::chrono::nanoseconds took
The time delta that this duration represents.
Definition: timer.h:55
auto microseconds(void)
Return the time-slice as number of microseconds.
Definition: timer.h:91
replacement for the C runtime library assert that also works in release.
static stop_watch start()
Start a stop watfh.
Definition: timer.h:119
static duration stop(stop_watch watch)
Return the time on the stop watch.
Definition: timer.h:133
#define JASS_assert(expression)
Drop in replacement for assert() that aborts in Release as well as Debug.
Definition: asserts.h:33
auto milliseconds(void)
Return the time-slice as number of milliseconds.
Definition: timer.h:78
General purpose cross-platform timer methods for C++11 and later.
Definition: timer.h:32
A duration as returned by a stop_watch.
Definition: timer.h:52
static void unittest(void)
Unit test this class.
Definition: timer.h:145
auto nanoseconds(void)
Return the time-slice as number of nanoseconds.
Definition: timer.h:104
duration(std::chrono::nanoseconds value)
Convert a std::chrono::nanoseconds into a JASS::timer::duration object.
Definition: timer.h:65
Definition: compress_integer_elias_delta_simd.c:23
std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > stop_watch
An instance of a timer.
Definition: timer.h:43