timer.h
Go to the documentation of this file.
00001 /*
00002 Copyright 2011. All rights reserved.
00003 Institute of Measurement and Control Systems
00004 Karlsruhe Institute of Technology, Germany
00005 
00006 This file is part of libviso2.
00007 Authors: Andreas Geiger
00008 
00009 libviso2 is free software; you can redistribute it and/or modify it under the
00010 terms of the GNU General Public License as published by the Free Software
00011 Foundation; either version 2 of the License, or any later version.
00012 
00013 libviso2 is distributed in the hope that it will be useful, but WITHOUT ANY
00014 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00015 PARTICULAR PURPOSE. See the GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License along with
00018 libviso2; if not, write to the Free Software Foundation, Inc., 51 Franklin
00019 Street, Fifth Floor, Boston, MA 02110-1301, USA 
00020 */
00021 
00022 #ifndef __TIMER_H__
00023 #define __TIMER_H__
00024 
00025 #include <iostream>
00026 #include <iomanip>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <vector>
00031 #include <string>
00032 #include <sys/time.h>
00033 #include <stdint.h>
00034 
00035 class Timer {
00036   
00037 public:
00038   
00039   Timer() {}
00040   
00041   ~Timer() {}
00042   
00043   void start (std::string title) {
00044     desc.push_back(title);
00045     push_back_time();
00046   }
00047   
00048   void stop () {
00049     if (time.size()<=desc.size())
00050       push_back_time();
00051   }
00052   
00053   void plot () {
00054     stop();
00055     float total_time = 0;
00056     for (int32_t i=0; i<(int32_t)desc.size(); i++) {
00057       float curr_time = getTimeDifferenceMilliseconds(time[i],time[i+1]);
00058       total_time += curr_time;
00059       std::cout.width(30);
00060       std::cout << desc[i] << " ";
00061       std::cout << std::fixed << std::setprecision(1) << std::setw(6);
00062       std::cout << curr_time;
00063       std::cout << " ms" << std::endl;
00064     }
00065     std::cout << "========================================" << std::endl;
00066     std::cout << "                    Total time ";
00067     std::cout << std::fixed << std::setprecision(1) << std::setw(6);
00068     std::cout << total_time;
00069     std::cout << " ms" << std::endl << std::endl;
00070   }
00071   
00072   void reset () {
00073     desc.clear();
00074     time.clear();
00075   }
00076   
00077 private:
00078   std::vector<std::string>  desc;
00079   std::vector<timeval>      time;
00080   
00081   void push_back_time () {
00082     timeval curr_time;
00083     gettimeofday(&curr_time,0);
00084     time.push_back(curr_time);
00085   }
00086   
00087   float getTimeDifferenceMilliseconds(timeval a,timeval b) {
00088     return ((float)(b.tv_sec -a.tv_sec ))*1e+3 +
00089            ((float)(b.tv_usec-a.tv_usec))*1e-3;
00090   }
00091 };
00092 
00093 #endif


libviso2
Author(s): Andreas Geiger, Stephan Wirth
autogenerated on Thu Jun 6 2019 21:23:13