libelas
timer.h
Go to the documentation of this file.
1 /*
2 Copyright 2011. All rights reserved.
3 Institute of Measurement and Control Systems
4 Karlsruhe Institute of Technology, Germany
5 
6 This file is part of libelas.
7 Authors: Andreas Geiger
8 
9 libelas is free software; you can redistribute it and/or modify it under the
10 terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 3 of the License, or any later version.
12 
13 libelas is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 
22 #ifndef __TIMER_H__
23 #define __TIMER_H__
24 
25 #include <iostream>
26 #include <iomanip>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <vector>
31 #include <string>
32 #include <sys/time.h>
33 
34 // Define fixed-width datatypes for Visual Studio projects
35 #ifndef _MSC_VER
36  #include <stdint.h>
37 #else
38  typedef __int8 int8_t;
39  typedef __int16 int16_t;
40  typedef __int32 int32_t;
41  typedef __int64 int64_t;
42  typedef unsigned __int8 uint8_t;
43  typedef unsigned __int16 uint16_t;
44  typedef unsigned __int32 uint32_t;
45  typedef unsigned __int64 uint64_t;
46 #endif
47 
48 class Timer {
49 
50 public:
51 
52  Timer() {}
53 
54  ~Timer() {}
55 
56  void start (std::string title) {
57  desc.push_back(title);
58  push_back_time();
59  }
60 
61  void stop () {
62  if (time.size()<=desc.size())
63  push_back_time();
64  }
65 
66  void plot () {
67  stop();
68  float total_time = 0;
69  for (int32_t i=0; i<desc.size(); i++) {
70  float curr_time = getTimeDifferenceMilliseconds(time[i],time[i+1]);
71  total_time += curr_time;
72  std::cout.width(30);
73  std::cout << desc[i] << " ";
74  std::cout << std::fixed << std::setprecision(1) << std::setw(6);
75  std::cout << curr_time;
76  std::cout << " ms" << std::endl;
77  }
78  std::cout << "========================================" << std::endl;
79  std::cout << " Total time ";
80  std::cout << std::fixed << std::setprecision(1) << std::setw(6);
81  std::cout << total_time;
82  std::cout << " ms" << std::endl << std::endl;
83  }
84 
85  void reset () {
86  desc.clear();
87  time.clear();
88  }
89 
90 private:
91 
92  std::vector<std::string> desc;
93  std::vector<timeval> time;
94 
95  void push_back_time () {
96  timeval curr_time;
97  gettimeofday(&curr_time,0);
98  time.push_back(curr_time);
99  }
100 
101  float getTimeDifferenceMilliseconds(timeval a,timeval b) {
102  return ((float)(b.tv_sec -a.tv_sec ))*1e+3 +
103  ((float)(b.tv_usec-a.tv_usec))*1e-3;
104  }
105 };
106 
107 #endif
void start(std::string title)
Definition: timer.h:56
void reset()
Definition: timer.h:85
~Timer()
Definition: timer.h:54
void plot()
Definition: timer.h:66
Timer()
Definition: timer.h:52
Definition: timer.h:48
void stop()
Definition: timer.h:61