Blender V2.61 - r43446

KX_TimeLogger.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "KX_TimeLogger.h"
00034 
00035 KX_TimeLogger::KX_TimeLogger(unsigned int maxNumMeasurements) : 
00036     m_maxNumMeasurements(maxNumMeasurements), 
00037     m_logStart(0),
00038     m_logging(false)
00039 {
00040 }
00041 
00042 
00043 KX_TimeLogger::~KX_TimeLogger(void)
00044 {
00045 }
00046 
00047 
00048 void KX_TimeLogger::SetMaxNumMeasurements(unsigned int maxNumMeasurements)
00049 {
00050     if ((m_maxNumMeasurements != maxNumMeasurements) && maxNumMeasurements) {
00051         // Actual removing is done in NextMeasurement()
00052         m_maxNumMeasurements = maxNumMeasurements;
00053     }
00054 }
00055 
00056 
00057 unsigned int KX_TimeLogger::GetMaxNumMeasurements(void) const
00058 {
00059     return m_maxNumMeasurements;
00060 }
00061 
00062 
00063 void KX_TimeLogger::StartLog(double now)
00064 {
00065     if (!m_logging) {
00066         m_logging = true;
00067         m_logStart = now;
00068     }
00069 }
00070 
00071 
00072 void KX_TimeLogger::EndLog(double now)
00073 {
00074     if (m_logging) {
00075         m_logging = false;
00076         double time = now - m_logStart;
00077         if (m_measurements.size() > 0) {
00078             m_measurements[0] += time;
00079         }
00080     }
00081 }
00082 
00083 
00084 void KX_TimeLogger::NextMeasurement(double now)
00085 {
00086     // End logging to current measurement
00087     EndLog(now);
00088 
00089     // Add a new measurement at the front
00090     double m = 0.;
00091     m_measurements.push_front(m);
00092 
00093     // Remove measurement if we grow beyond the maximum size
00094     if ((m_measurements.size()) > m_maxNumMeasurements) {
00095         while (m_measurements.size() > m_maxNumMeasurements) {
00096             m_measurements.pop_back();
00097         }
00098     }
00099 }
00100 
00101 
00102 
00103 double KX_TimeLogger::GetAverage(void) const
00104 {
00105     double avg = 0.;
00106 
00107     unsigned int numMeasurements = m_measurements.size();
00108     if (numMeasurements > 1) {
00109         for (unsigned int i = 1; i < numMeasurements; i++) {
00110             avg += m_measurements[i];
00111         }
00112         avg /= (float)numMeasurements - 1;
00113     }
00114 
00115     return avg;
00116 }
00117