|
MASA-Core
|
00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2010-2015 Edans Sandes 00004 * 00005 * This file is part of MASA-Core. 00006 * 00007 * MASA-Core is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * MASA-Core is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with MASA-Core. If not, see <http://www.gnu.org/licenses/>. 00019 * 00020 ******************************************************************************/ 00021 00022 #include "Timer.hpp" 00023 00024 #include <stdio.h> 00025 00026 bool Timer::hasStaticEvent = false; 00027 timeval Timer::staticEvent; 00028 00029 float Timer::getGlobalTime() { 00030 if (!hasStaticEvent) { 00031 hasStaticEvent = true; 00032 gettimeofday(&staticEvent, NULL); 00033 } 00034 00035 timeval event; 00036 gettimeofday(&event, NULL); 00037 00038 float diff = getElapsedTime(&event, &staticEvent); 00039 return diff; 00040 } 00041 00042 00043 float Timer::getElapsedTime(timeval *end_time, timeval *start_time) { 00044 timeval temp_diff; 00045 00046 temp_diff.tv_sec = end_time->tv_sec - start_time->tv_sec; 00047 temp_diff.tv_usec = end_time->tv_usec - start_time->tv_usec; 00048 00049 if (temp_diff.tv_usec < 0) { 00050 long long nsec = temp_diff.tv_usec/1000000; 00051 temp_diff.tv_usec += 1000000 * nsec; 00052 temp_diff.tv_sec -= nsec; 00053 } 00054 00055 return (1000000LL * temp_diff.tv_sec + temp_diff.tv_usec)/1000.0f; 00056 00057 } 00058 00059 Timer::Timer() { 00060 /*unsigned int timer = 0; 00061 cutilCheckError( cutCreateTimer( &timer)); 00062 cutilCheckError( cutStartTimer( timer));*/ 00063 hasEvent = 0; 00064 //previousEvent = NULL; 00065 00066 gettimeofday(&startEvent, NULL); 00067 gettimeofday(&intervalEvent, NULL); 00068 gettimeofday(&previousEvent, NULL); 00069 00070 //init(); 00071 } 00072 00073 Timer::~Timer() { 00074 } 00075 00076 int Timer::createEvent(string name) { 00077 stat_t stat; 00078 stat.name = name; 00079 stat.sum = 0; 00080 stat.count = 0; 00081 int id = stats.size(); 00082 stats.push_back(stat); 00083 00084 return id; 00085 00086 } 00087 00088 void Timer::init() { 00089 gettimeofday(&startEvent, NULL); 00090 gettimeofday(&intervalEvent, NULL); 00091 gettimeofday(&previousEvent, NULL); 00092 hasEvent = true; 00093 } 00094 00095 float Timer::eventRecord(int id) { 00096 float diff = 0; 00097 timeval currentEvent = stats[id].event; 00098 00099 gettimeofday(¤tEvent, NULL); 00100 00101 if (hasEvent) { 00102 diff = getElapsedTime(¤tEvent, &previousEvent); 00103 stats[id].sum += diff; 00104 stats[id].count++; 00105 } 00106 previousEvent = currentEvent; 00107 hasEvent = true; 00108 return diff; 00109 } 00110 00111 float Timer::totalTime() { 00112 timeval currentEvent; 00113 gettimeofday(¤tEvent, NULL); 00114 float diff = getElapsedTime(¤tEvent, &startEvent); 00115 return diff; 00116 } 00117 00118 bool Timer::intervalElapsed ( float interval ) { 00119 timeval currentEvent; 00120 gettimeofday(¤tEvent, NULL); 00121 float diff = getElapsedTime(¤tEvent, &intervalEvent); 00122 if (diff/1000 >= interval) { 00123 //printf("OK||||||\n"); 00124 intervalEvent = currentEvent; 00125 return true; 00126 } else { 00127 //printf("%f - %f\n", diff/1000, interval); 00128 return false; 00129 } 00130 } 00131 00132 00133 float Timer::printStatistics(FILE* file) { 00134 float sum = 0; 00135 for (int i = 0; i<stats.size(); i++) { 00136 stat_t stat = stats[i]; 00137 fprintf(file, "%15s: %12.4f (%4d) avg.: %8.4f\n", stat.name.c_str(), stat.sum, stat.count, stat.sum/stat.count); 00138 sum += stat.sum; 00139 } 00140 fprintf(file, "%15s: %12.4f\n", "TOTAL", sum); 00141 fflush(file); 00142 return sum; 00143 } 00144 00145
1.7.6.1