Directory: | ./ |
---|---|
File: | include/pinocchio/utils/timer.hpp |
Date: | 2025-02-12 21:03:38 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 21 | 28 | 75.0% |
Branches: | 4 | 19 | 21.1% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2015-2019 CNRS INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef __pinocchio_utils_timer_hpp__ | ||
6 | #define __pinocchio_utils_timer_hpp__ | ||
7 | |||
8 | #ifdef WIN32 | ||
9 | #include <Windows.h> | ||
10 | #include <stdint.h> // portable: uint64_t MSVC: __int64 | ||
11 | |||
12 | int gettimeofday(struct timeval * tp, struct timezone * tzp) | ||
13 | { | ||
14 | // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's | ||
15 | // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC) | ||
16 | // until 00:00:00 January 1, 1970 | ||
17 | static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL); | ||
18 | |||
19 | SYSTEMTIME system_time; | ||
20 | FILETIME file_time; | ||
21 | uint64_t time; | ||
22 | |||
23 | GetSystemTime(&system_time); | ||
24 | SystemTimeToFileTime(&system_time, &file_time); | ||
25 | time = ((uint64_t)file_time.dwLowDateTime); | ||
26 | time += ((uint64_t)file_time.dwHighDateTime) << 32; | ||
27 | |||
28 | tp->tv_sec = (long)((time - EPOCH) / 10000000L); | ||
29 | tp->tv_usec = (long)(system_time.wMilliseconds * 1000); | ||
30 | return 0; | ||
31 | } | ||
32 | #else | ||
33 | #include <sys/time.h> | ||
34 | #endif | ||
35 | #include <iostream> | ||
36 | #include <stack> | ||
37 | |||
38 | #define SMOOTH(s) for (size_t _smooth = 0; _smooth < s; ++_smooth) | ||
39 | |||
40 | /* Return the time spent in secs. */ | ||
41 | 16 | inline double operator-(const struct timeval & t1, const struct timeval & t0) | |
42 | { | ||
43 | /* TODO: double check the double conversion from long (on 64x). */ | ||
44 | 16 | return double(t1.tv_sec - t0.tv_sec) + 1e-6 * double(t1.tv_usec - t0.tv_usec); | |
45 | } | ||
46 | |||
47 | struct PinocchioTicToc | ||
48 | { | ||
49 | enum Unit | ||
50 | { | ||
51 | S = 1, | ||
52 | MS = 1000, | ||
53 | US = 1000000, | ||
54 | NS = 1000000000 | ||
55 | }; | ||
56 | Unit DEFAULT_UNIT; | ||
57 | |||
58 | 16 | static std::string unitName(Unit u) | |
59 | { | ||
60 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
16 | switch (u) |
61 | { | ||
62 | ✗ | case S: | |
63 | ✗ | return "s"; | |
64 | ✗ | case MS: | |
65 | ✗ | return "ms"; | |
66 | 16 | case US: | |
67 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | return "us"; |
68 | ✗ | case NS: | |
69 | ✗ | return "ns"; | |
70 | } | ||
71 | ✗ | return ""; | |
72 | } | ||
73 | |||
74 | std::stack<struct timeval> stack; | ||
75 | mutable struct timeval t0; | ||
76 | |||
77 | 7 | PinocchioTicToc(Unit def = MS) | |
78 | 7 | : DEFAULT_UNIT(def) | |
79 | { | ||
80 | 7 | } | |
81 | |||
82 | 16 | inline void tic() | |
83 | { | ||
84 | 16 | stack.push(t0); | |
85 | 16 | gettimeofday(&(stack.top()), NULL); | |
86 | 16 | } | |
87 | |||
88 | inline double toc() | ||
89 | { | ||
90 | return toc(DEFAULT_UNIT); | ||
91 | }; | ||
92 | |||
93 | 16 | inline double toc(const Unit factor) | |
94 | { | ||
95 | 16 | gettimeofday(&t0, NULL); | |
96 | 16 | double dt = (t0 - stack.top()) * (double)factor; | |
97 | 16 | stack.pop(); | |
98 | 16 | return dt; | |
99 | } | ||
100 | |||
101 | 16 | inline void toc(std::ostream & os, double SMOOTH = 1) | |
102 | { | ||
103 |
2/4✓ Branch 5 taken 16 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
|
16 | os << toc(DEFAULT_UNIT) / SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl; |
104 | 16 | } | |
105 | }; | ||
106 | |||
107 | #endif // ifndef __pinocchio_utils_timer_hpp__ | ||
108 |