MLIB
Loading...
Searching...
No Matches
stopwatch.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include <chrono>
15
16namespace mlib {
17
19class stopwatch
20{
21public:
22 stopwatch ();
23 void start ();
24 void stop ();
25 std::chrono::steady_clock::duration lap ();
26 std::chrono::steady_clock::duration end ();
27
28 double lap_msec ();
29 double end_msec ();
30
31private:
32 typedef std::chrono::steady_clock::time_point tpoint;
33 tpoint tbeg, tend;
34};
35
36inline
37stopwatch::stopwatch ()
38{
39}
40
42inline
44{
45 tbeg = std::chrono::steady_clock::now ();
46 tend = tpoint{};
47}
48
50inline
52{
53 tend = std::chrono::steady_clock::now ();
54}
55
56
62inline
63std::chrono::steady_clock::duration stopwatch::lap ()
64{
65 return (std::chrono::steady_clock::now () - tbeg);
66}
67
68
70inline
71std::chrono::steady_clock::duration stopwatch::end ()
72{
73 return (tend - tbeg);
74}
75
81inline
83{
84 return std::chrono::duration<double, std::milli> (lap ()).count();
85}
86
87
89inline
91{
92 return std::chrono::duration<double, std::milli> (tend - tbeg).count ();
93}
94
95
96} // namespace mlib
double lap_msec()
Return number of milliseconds elapsed from start.
Definition stopwatch.h:82
double end_msec()
Return total duration in milliseconds between start and stop.
Definition stopwatch.h:90
void start()
Start the stopwatch.
Definition stopwatch.h:43
std::chrono::steady_clock::duration lap()
Return elapsed time from start.
Definition stopwatch.h:63
void stop()
Stop the stopwatch.
Definition stopwatch.h:51
std::chrono::steady_clock::duration end()
Return total duration.
Definition stopwatch.h:71