1 /******************************************************************************\
2 * Copyright (c) 2016, Robert van Engelen, Genivia Inc. All rights reserved. *
3 * *
4 * Redistribution and use in source and binary forms, with or without *
5 * modification, are permitted provided that the following conditions are met: *
6 * *
7 * (1) Redistributions of source code must retain the above copyright notice, *
8 * this list of conditions and the following disclaimer. *
9 * *
10 * (2) Redistributions in binary form must reproduce the above copyright *
11 * notice, this list of conditions and the following disclaimer in the *
12 * documentation and/or other materials provided with the distribution. *
13 * *
14 * (3) The name of the author may not be used to endorse or promote products *
15 * derived from this software without specific prior written permission. *
16 * *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED *
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, *
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR *
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
27 \******************************************************************************/
28
29 /**
30 @file timer.h
31 @brief Measure elapsed wall-clock time in milliseconds
32 @author Robert van Engelen - engelen@genivia.com
33 @copyright (c) 2016-2020, Robert van Engelen, Genivia Inc. All rights reserved.
34 @copyright (c) BSD-3 License - see LICENSE.txt
35 */
36
37 #ifndef REFLEX_TIMER_H
38 #define REFLEX_TIMER_H
39
40 #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__BORLANDC__)
41
42 #include <windows.h>
43
44 namespace reflex {
45
46 typedef SYSTEMTIME timer_type;
47
48 /// Start timer.
timer_start(timer_type & t)49 inline void timer_start(timer_type& t) ///< timer to be initialized
50 {
51 GetLocalTime(&t);
52 }
53
54 /// Return elapsed time in milliseconds (ms) since the last call up to 1 minute (wraps if elapsed time exceeds 1 minute!)
timer_elapsed(timer_type & t)55 inline float timer_elapsed(timer_type& t) ///< timer to be updated
56 {
57 timer_type now;
58 GetLocalTime(&now);
59 float sec = now.wMilliseconds;
60 sec -= t.wMilliseconds;
61 t.wMilliseconds = now.wMilliseconds;
62 sec += 1000.0f * (now.wSecond - t.wSecond);
63 t.wSecond = now.wSecond;
64 if (sec < 0.0)
65 sec += 60000.0;
66 return sec;
67 }
68
69 } // namespace reflex
70
71 #else
72
73 #include <cstddef>
74 #include <sys/time.h>
75
76 namespace reflex {
77
78 typedef timeval timer_type;
79
80 /// Start timer.
timer_start(timer_type & t)81 inline void timer_start(timer_type& t) ///< timer to be initialized
82 {
83 gettimeofday(&t, NULL);
84 }
85
86 /// Return elapsed time in milliseconds (ms) with microsecond precision since the last call up to 1 minute (wraps if elapsed time exceeds 1 minute!)
timer_elapsed(timer_type & t)87 inline float timer_elapsed(timer_type& t) ///< timer to be updated
88 {
89 timer_type now;
90 gettimeofday(&now, NULL);
91 float sec = now.tv_usec;
92 sec -= t.tv_usec;
93 t.tv_usec = now.tv_usec;
94 sec = 1000.0 * (now.tv_sec - t.tv_sec) + sec/1000.0;
95 t.tv_sec = now.tv_sec;
96 if (sec < 0.0)
97 sec += 60000.0;
98 return sec;
99 }
100
101 } // namespace reflex
102
103 #endif
104
105 #endif
106