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