GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/pinocchio/utils/timer.hpp Lines: 17 18 94.4 %
Date: 2024-01-23 21:41:47 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 { S = 1, MS = 1000, US = 1000000, NS = 1000000000 };
50
  Unit DEFAULT_UNIT;
51
52
16
  static std::string unitName(Unit u)
53
  {
54



16
    switch(u) { case S: return "s"; case MS: return "ms"; case US: return "us"; case NS: return "ns"; }
55
    return "";
56
  }
57
58
  std::stack<struct timeval> stack;
59
  mutable struct timeval t0;
60
61
7
  PinocchioTicToc( Unit def = MS ) : DEFAULT_UNIT(def) {}
62
63
16
  inline void tic() {
64
16
    stack.push(t0);
65
16
    gettimeofday(&(stack.top()),NULL);
66
16
  }
67
68
  inline double toc() { return toc(DEFAULT_UNIT); };
69
70
16
  inline double toc(const Unit factor)
71
  {
72
16
    gettimeofday(&t0,NULL);
73
16
    double dt = (t0-stack.top())*(double)factor;
74
16
    stack.pop();
75
16
    return dt;
76
  }
77
78
16
  inline void toc(std::ostream & os, double SMOOTH=1)
79
  {
80

16
    os << toc(DEFAULT_UNIT)/SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl;
81
16
  }
82
};
83
84
#endif // ifndef __pinocchio_utils_timer_hpp__