Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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