00001 //###################################################################### 00002 // 00003 // GraspIt! 00004 // Copyright (C) 2002-2009 Columbia University in the City of New York. 00005 // All rights reserved. 00006 // 00007 // GraspIt! 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 // GraspIt! 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 GraspIt!. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // Author(s): Matei T. Ciocarlie 00021 // 00022 // $Id: profiling.cpp,v 1.6.4.1 2009/07/23 21:18:02 cmatei Exp $ 00023 // 00024 //###################################################################### 00025 00026 #include "profiling.h" 00027 00028 namespace Profiling { 00029 00030 ProfileInstance::ProfileInstance() 00031 { 00032 mCount = -1; 00033 mRunning = false; 00034 mName = "UNNAMED"; 00035 } 00036 00037 double 00038 ProfileInstance::getTotalTimeMicroseconds() 00039 { 00040 if (mRunning) 00041 { 00042 stopTimer(); 00043 startTimer(); 00044 } 00045 double result; 00046 PROF_CONVERT_TO_MICROS(mElapsedTime, result); 00047 return result; 00048 } 00049 00050 void ProfileInstance::print() 00051 { 00052 double totalTime = getTotalTimeMicroseconds(); 00053 if (mCount == 0 && totalTime <= 0) return; 00054 std::cerr << mName << ": "; 00055 if (mCount > 0) std::cerr << "Count is "<< mCount << "; "; 00056 if (totalTime > 0) 00057 { 00058 std::cerr << "Time is " << ((float)totalTime)/1000 << "ms"; 00059 if (mRunning) std::cerr << " (still running)"; 00060 std::cerr << "; "; 00061 } 00062 std::cerr << std::endl; 00063 } 00064 00065 Profiler::Profiler() 00066 { 00067 mSize = 0; 00068 resize(10); 00069 mNextIndex = 0; 00070 #ifdef WIN32 00071 LARGE_INTEGER tmp; 00072 QueryPerformanceFrequency(&tmp); 00073 COUNTS_PER_SEC = tmp.QuadPart; 00074 if (COUNTS_PER_SEC==0) 00075 { 00076 std::cerr << "High performance timer not availabled! PROFILER NOT OPERATIONAL.\n"; 00077 COUNTS_PER_SEC = 1; 00078 } 00079 #endif 00080 } 00081 00082 Profiler::~Profiler() 00083 { 00084 } 00085 00086 void Profiler::resize(int size) 00087 { 00088 if (size <= mSize) return; 00089 mSize = size; 00090 mPI.resize(mSize,ProfileInstance()); 00091 } 00092 00093 int Profiler::getNewIndex(const char *name) 00094 { 00095 if (mNextIndex >= mSize) 00096 { 00097 resize(2*mSize); 00098 } 00099 mPI[mNextIndex].setName(name); 00100 mPI[mNextIndex].reset(); 00101 return mNextIndex++; 00102 } 00103 00104 void Profiler::resetAll() 00105 { 00106 for (int i=0; i<mNextIndex; i++) 00107 { 00108 mPI[i].reset(); 00109 } 00110 } 00111 00112 void Profiler::printAll() 00113 { 00114 for (int i=0; i<mNextIndex; i++) 00115 { 00116 mPI[i].print(); 00117 } 00118 } 00119 00120 }