stereo-vision
All Data Structures Namespaces Functions Modules Pages
timer.h
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 <stdint.h>
31 #include <vector>
32 #include <string>
33 #include <sys/time.h>
34 
35 class Timer {
36 
37 public:
38 
39  Timer() {}
40 
41  ~Timer() {}
42 
43  void start (std::string title) {
44  desc.push_back(title);
45  push_back_time();
46  }
47 
48  void stop () {
49  if (time.size()<=desc.size())
50  push_back_time();
51  }
52 
53  void plot () {
54  stop();
55  float total_time = 0;
56  for (int32_t i=0; i<desc.size(); i++) {
57  float curr_time = getTimeDifferenceMilliseconds(time[i],time[i+1]);
58  total_time += curr_time;
59  std::cout.width(30);
60  std::cout << desc[i] << " ";
61  std::cout << std::fixed << std::setprecision(1) << std::setw(6);
62  std::cout << curr_time;
63  std::cout << " ms" << std::endl;
64  }
65  std::cout << "========================================" << std::endl;
66  std::cout << " Total time ";
67  std::cout << std::fixed << std::setprecision(1) << std::setw(6);
68  std::cout << total_time;
69  std::cout << " ms" << std::endl << std::endl;
70  }
71 
72  void reset () {
73  desc.clear();
74  time.clear();
75  }
76 
77 private:
78 
79  std::vector<std::string> desc;
80  std::vector<timeval> time;
81 
82  void push_back_time () {
83  timeval curr_time;
84  gettimeofday(&curr_time,0);
85  time.push_back(curr_time);
86  }
87 
88  float getTimeDifferenceMilliseconds(timeval a,timeval b) {
89  return ((float)(b.tv_sec -a.tv_sec ))*1e+3 +
90  ((float)(b.tv_usec-a.tv_usec))*1e-3;
91  }
92 };
93 
94 #endif