00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #include <ctime>
00029 #include <iostream>
00030 #include <vector>
00031 #include <string>
00032
00033 using namespace std;
00034
00035
00036 class my_Time
00037 {
00038
00039 private:
00040
00041 int days;
00042 int hours;
00043 int minutes;
00044 int seconds;
00045 string function;
00046 double totalSeconds;
00047 vector <double>totalTime;
00048 double average;
00049 time_t starttime;
00050 time_t stoptime;
00051
00052 public:
00053
00054 my_Time(string f,int d = 0, int h = 0, int m = 0, int s = 0, double a = 0)
00055 {
00056 days = d;
00057 hours = h;
00058 minutes = m;
00059 seconds = s;
00060 average = a;
00061 function = f;
00062 }
00063 int totalMinutes(double &s)
00064 {
00065 int m = 0;
00066 m =(int)(s/60);
00067 int sec = (int)s;
00068 if(s>60)s = (double)(sec%60);
00069 return m;
00070 }
00071
00072 int totalHours(int &m)
00073 {
00074 int h = 0;
00075 h = (int)(m/60);
00076 if(m>60)m = m%60;
00077 return h;
00078 }
00079
00080 int totalDays(int &h)
00081 {
00082 int d = 0;
00083 d = (int)(h/24);
00084 if(h>24)h = h%24;
00085
00086 return d;
00087 }
00088 void convert(double s)
00089 {
00090 minutes = totalMinutes(s);
00091 hours = totalHours(minutes);
00092 days = totalDays(hours);
00093
00094 seconds = (int)s;
00095 }
00096
00097
00098
00099 ~my_Time(){};
00100
00101 void printTime(ostream &fstr)
00102 {
00103 fstr<<"Total time taken for function "<<function<<" is ";
00104 fstr<<days<<" days, ";
00105 fstr<<hours<<" hours, ";
00106 fstr<<minutes<<" minutes and ";
00107 fstr<< seconds<<" seconds."<<endl;
00108 if(average)
00109 fstr<<"The average time taken is "<<average<<" seconds."<<endl;
00110 }
00111 void start(void)
00112 {
00113 time(&starttime);
00114 }
00115 void stop_once(void)
00116 {
00117 time(&stoptime);
00118 }
00119 void stop(void)
00120 {
00121 time(&stoptime);
00122 totalSeconds = difftime(stoptime,starttime);
00123 totalTime.push_back(totalSeconds);
00124 }
00125 void timeTaken(void)
00126 {
00127 for(uint i = 0;i<totalTime.size();++i)
00128 {
00129 totalSeconds = totalTime[i] + totalSeconds;
00130 }
00131 convert(totalSeconds);
00132 average = averageTime();
00133 }
00134 double averageTime(void)
00135 {
00136 average = 0;
00137 for(uint i = 0;i<totalTime.size();++i)
00138 {
00139 average = totalTime[i] + average;
00140 }
00141 if(average) average = average / totalTime.size();
00142 return average;
00143 }
00144 void timeTaken_once(void)
00145 {
00146 totalSeconds = difftime(stoptime,starttime);
00147 convert(totalSeconds);
00148 }
00149
00150
00151 };
00152
00153