00001 /* 00002 * utilite is a cross-platform library with 00003 * useful utilities for fast and small developing. 00004 * Copyright (C) 2010 Mathieu Labbe 00005 * 00006 * utilite is free library: you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * utilite is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "rtabmap/utilite/UTimer.h" 00021 #include "rtabmap/utilite/ULogger.h" 00022 00024 // public: 00026 UTimer::UTimer() 00027 { 00028 #ifdef _WIN32 00029 QueryPerformanceFrequency(&frequency_); 00030 #endif 00031 start(); // This will initialize the private counters 00032 } 00033 00034 UTimer::~UTimer() {} 00035 00036 #ifdef _WIN32 00037 double UTimer::now() 00038 { 00039 LARGE_INTEGER count, freq; 00040 QueryPerformanceFrequency(&freq); 00041 QueryPerformanceCounter(&count); 00042 return double(count.QuadPart) / freq.QuadPart; 00043 } 00044 00045 void UTimer::start() 00046 { 00047 QueryPerformanceCounter(&startTimeRecorded_); 00048 stopTimeRecorded_ = startTimeRecorded_; 00049 } 00050 void UTimer::stop() 00051 { 00052 QueryPerformanceCounter(&stopTimeRecorded_); 00053 00054 } 00055 double UTimer::getElapsedTime() 00056 { 00057 LARGE_INTEGER now; 00058 QueryPerformanceCounter(&now); 00059 return double(now.QuadPart - startTimeRecorded_.QuadPart) / frequency_.QuadPart; 00060 } 00061 double UTimer::getInterval() 00062 { 00063 if(stopTimeRecorded_.QuadPart == startTimeRecorded_.QuadPart) 00064 { 00065 return getElapsedTime(); 00066 } 00067 else 00068 { 00069 return double(stopTimeRecorded_.QuadPart - startTimeRecorded_.QuadPart) / frequency_.QuadPart; 00070 } 00071 } 00072 #else 00073 double UTimer::now() 00074 { 00075 struct timeval tv; 00076 gettimeofday(&tv, NULL); 00077 return double(tv.tv_sec) + double(tv.tv_usec) / 1000000.0; 00078 } 00079 00080 void UTimer::start() 00081 { 00082 gettimeofday(&startTimeRecorded_, NULL); 00083 stopTimeRecorded_ = startTimeRecorded_; 00084 } 00085 void UTimer::stop() 00086 { 00087 gettimeofday(&stopTimeRecorded_, NULL); 00088 00089 } 00090 double UTimer::getElapsedTime() 00091 { 00092 return UTimer::now() - (double(startTimeRecorded_.tv_sec) + double(startTimeRecorded_.tv_usec) / 1000000.0); 00093 00094 } 00095 double UTimer::getInterval() 00096 { 00097 if(startTimeRecorded_.tv_sec == stopTimeRecorded_.tv_sec && startTimeRecorded_.tv_usec == stopTimeRecorded_.tv_usec) 00098 { 00099 return getElapsedTime(); 00100 } 00101 else 00102 { 00103 double start = double(startTimeRecorded_.tv_sec) + double(startTimeRecorded_.tv_usec) / 1000000.0; 00104 double stop = double(stopTimeRecorded_.tv_sec) + double(stopTimeRecorded_.tv_usec) / 1000000.0; 00105 return stop - start; 00106 } 00107 } 00108 #endif 00109 00110 double UTimer::ticks() // Stop->start and return Interval 00111 { 00112 double inter = elapsed(); 00113 start(); 00114 return inter; 00115 }