Developer documentation
Version 3.0.3-105-gd3941f44
timer.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __timer_h__
18#define __timer_h__
19
20#include <chrono>
21
22#define NOMEMALIGN
23
24namespace MR
25{
26
28 public:
29 Timer () {
30 start();
31 }
32
33 void start () {
34 from = std::chrono::high_resolution_clock::now();
35 }
36 double elapsed () {
37 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - from).count() * 1.0e-9;
38 }
39
40 static double current_time () {
41 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() * 1.0e-9;
42 }
43
44 protected:
45 std::chrono::high_resolution_clock::time_point from;
46 };
47
48
49
50
51 // a class to help perform operations at given time intervals
52 class IntervalTimer : protected Timer { NOMEMALIGN
53 public:
55 IntervalTimer (double time_interval = 0.0333333) :
56 interval (std::chrono::duration_cast<std::chrono::high_resolution_clock::duration> (std::chrono::nanoseconds (std::chrono::nanoseconds::rep (1.0e9*time_interval)))),
57 next_time (from + interval) { }
58
60
62 operator bool() {
63 auto now = std::chrono::high_resolution_clock::now();
64 if (now < next_time)
65 return false;
66 from = now;
68 return true;
69 }
70
71 protected:
72 const std::chrono::high_resolution_clock::duration interval;
73 std::chrono::high_resolution_clock::time_point next_time;
74 };
75
76}
77
78#endif
79
IntervalTimer(double time_interval=0.0333333)
by default, fire at ~30 Hz - most monitors are 60Hz
Definition: timer.h:55
std::chrono::high_resolution_clock::time_point next_time
Definition: timer.h:73
const std::chrono::high_resolution_clock::duration interval
Definition: timer.h:72
Timer()
Definition: timer.h:29
void start()
Definition: timer.h:33
std::chrono::high_resolution_clock::time_point from
Definition: timer.h:45
double elapsed()
Definition: timer.h:36
static double current_time()
Definition: timer.h:40
Definition: base.h:24
Definition: types.h:303
#define NOMEMALIGN
Definition: timer.h:22