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 |
|
✗ |
inline double operator-(const struct timeval & t1, const struct timeval & t0) |
42 |
|
|
{ |
43 |
|
|
/* TODO: double check the double conversion from long (on 64x). */ |
44 |
|
✗ |
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 |
|
✗ |
static std::string unitName(Unit u) |
59 |
|
|
{ |
60 |
|
✗ |
switch (u) |
61 |
|
|
{ |
62 |
|
✗ |
case S: |
63 |
|
✗ |
return "s"; |
64 |
|
✗ |
case MS: |
65 |
|
✗ |
return "ms"; |
66 |
|
✗ |
case US: |
67 |
|
✗ |
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 |
|
✗ |
PinocchioTicToc(Unit def = MS) |
78 |
|
✗ |
: DEFAULT_UNIT(def) |
79 |
|
|
{ |
80 |
|
|
} |
81 |
|
|
|
82 |
|
✗ |
inline void tic() |
83 |
|
|
{ |
84 |
|
✗ |
stack.push(t0); |
85 |
|
✗ |
gettimeofday(&(stack.top()), NULL); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
inline double toc() |
89 |
|
|
{ |
90 |
|
|
return toc(DEFAULT_UNIT); |
91 |
|
|
}; |
92 |
|
|
|
93 |
|
✗ |
inline double toc(const Unit factor) |
94 |
|
|
{ |
95 |
|
✗ |
gettimeofday(&t0, NULL); |
96 |
|
✗ |
double dt = (t0 - stack.top()) * (double)factor; |
97 |
|
✗ |
stack.pop(); |
98 |
|
✗ |
return dt; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
✗ |
inline void toc(std::ostream & os, double SMOOTH = 1) |
102 |
|
|
{ |
103 |
|
✗ |
os << toc(DEFAULT_UNIT) / SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl; |
104 |
|
|
} |
105 |
|
|
}; |
106 |
|
|
|
107 |
|
|
#endif // ifndef __pinocchio_utils_timer_hpp__ |
108 |
|
|
|