|
a |
|
b/src/chrono.cpp |
|
|
1 |
#ifndef TEST_CHRONO
|
|
|
2 |
/* Copyright (C) 2014 J.F.Dockes
|
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
|
4 |
* it under the terms of the GNU General Public License as published by
|
|
|
5 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
* (at your option) any later version.
|
|
|
7 |
*
|
|
|
8 |
* This program is distributed in the hope that it will be useful,
|
|
|
9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
* GNU General Public License for more details.
|
|
|
12 |
*
|
|
|
13 |
* You should have received a copy of the GNU General Public License
|
|
|
14 |
* along with this program; if not, write to the
|
|
|
15 |
* Free Software Foundation, Inc.,
|
|
|
16 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
// Measure and display time intervals.
|
|
|
20 |
|
|
|
21 |
#include "chrono.h"
|
|
|
22 |
|
|
|
23 |
#include <chrono>
|
|
|
24 |
|
|
|
25 |
using namespace std;
|
|
|
26 |
|
|
|
27 |
Chrono::TimePoint Chrono::o_now;
|
|
|
28 |
|
|
|
29 |
void Chrono::refnow()
|
|
|
30 |
{
|
|
|
31 |
o_now = chrono::steady_clock::now();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
Chrono::Chrono()
|
|
|
35 |
: m_orig(chrono::steady_clock::now())
|
|
|
36 |
{
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
long Chrono::restart()
|
|
|
40 |
{
|
|
|
41 |
auto nnow = chrono::steady_clock::now();
|
|
|
42 |
auto ms =
|
|
|
43 |
chrono::duration_cast<chrono::milliseconds>(nnow - m_orig);
|
|
|
44 |
m_orig = nnow;
|
|
|
45 |
return ms.count();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
long Chrono::urestart()
|
|
|
49 |
{
|
|
|
50 |
auto nnow = chrono::steady_clock::now();
|
|
|
51 |
auto ms =
|
|
|
52 |
chrono::duration_cast<chrono::microseconds>(nnow - m_orig);
|
|
|
53 |
m_orig = nnow;
|
|
|
54 |
return ms.count();
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
long Chrono::millis(bool frozen)
|
|
|
58 |
{
|
|
|
59 |
if (frozen) {
|
|
|
60 |
return chrono::duration_cast<chrono::milliseconds>
|
|
|
61 |
(o_now - m_orig).count();
|
|
|
62 |
} else {
|
|
|
63 |
return chrono::duration_cast<chrono::milliseconds>
|
|
|
64 |
(chrono::steady_clock::now() - m_orig).count();
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
long Chrono::micros(bool frozen)
|
|
|
69 |
{
|
|
|
70 |
if (frozen) {
|
|
|
71 |
return chrono::duration_cast<chrono::microseconds>
|
|
|
72 |
(o_now - m_orig).count();
|
|
|
73 |
} else {
|
|
|
74 |
return chrono::duration_cast<chrono::microseconds>
|
|
|
75 |
(chrono::steady_clock::now() - m_orig).count();
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
float Chrono::secs(bool frozen)
|
|
|
80 |
{
|
|
|
81 |
if (frozen) {
|
|
|
82 |
return chrono::duration_cast<chrono::seconds>
|
|
|
83 |
(o_now - m_orig).count();
|
|
|
84 |
} else {
|
|
|
85 |
return (chrono::duration_cast<chrono::seconds>
|
|
|
86 |
(chrono::steady_clock::now() - m_orig)).count();
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
#else
|
|
|
91 |
|
|
|
92 |
// Test
|
|
|
93 |
#include <stdio.h>
|
|
|
94 |
#include <signal.h>
|
|
|
95 |
#include <unistd.h>
|
|
|
96 |
#include <stdlib.h>
|
|
|
97 |
|
|
|
98 |
#include "chrono.h"
|
|
|
99 |
|
|
|
100 |
using namespace std;
|
|
|
101 |
|
|
|
102 |
static char *thisprog;
|
|
|
103 |
static void
|
|
|
104 |
Usage(void)
|
|
|
105 |
{
|
|
|
106 |
fprintf(stderr, "Usage : %s \n", thisprog);
|
|
|
107 |
exit(1);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
Chrono achrono;
|
|
|
111 |
Chrono rchrono;
|
|
|
112 |
|
|
|
113 |
void
|
|
|
114 |
showsecs(long msecs)
|
|
|
115 |
{
|
|
|
116 |
fprintf(stderr, "%3.5f S", (double(msecs)) / 1000.0);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
void
|
|
|
120 |
sigint(int sig)
|
|
|
121 |
{
|
|
|
122 |
signal(SIGINT, sigint);
|
|
|
123 |
signal(SIGQUIT, sigint);
|
|
|
124 |
|
|
|
125 |
fprintf(stderr, "Absolute interval: ");
|
|
|
126 |
showsecs(achrono.millis());
|
|
|
127 |
fprintf(stderr, ". Relative interval: ");
|
|
|
128 |
showsecs(rchrono.restart());
|
|
|
129 |
fprintf(stderr, ".\n");
|
|
|
130 |
if (sig == SIGQUIT) {
|
|
|
131 |
exit(0);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
int main(int argc, char **argv)
|
|
|
136 |
{
|
|
|
137 |
thisprog = argv[0];
|
|
|
138 |
argc--; argv++;
|
|
|
139 |
if (argc != 0) {
|
|
|
140 |
Usage();
|
|
|
141 |
}
|
|
|
142 |
sleep(1);
|
|
|
143 |
fprintf(stderr, "Initial micros: %ld\n", achrono.micros());;
|
|
|
144 |
fprintf(stderr, "Type ^C for intermediate result, ^\\ to stop\n");
|
|
|
145 |
signal(SIGINT, sigint);
|
|
|
146 |
signal(SIGQUIT, sigint);
|
|
|
147 |
achrono.restart();
|
|
|
148 |
rchrono.restart();
|
|
|
149 |
while (1) {
|
|
|
150 |
pause();
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
#endif /*TEST_CHRONO*/
|