Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Class Members | File Members

PerformanceTimer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   SpikeStream Library                                                   *
00003  *   Copyright (C) 2007 by David Gamez                                     *
00004  *   david@davidgamez.eu                                                   *
00005  *   Version 0.1                                                           *
00006  *                                                                         *
00007  *   This program 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 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program 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 this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 //SpikeStream includes
00024 #include "PerformanceTimer.h"
00025 
00026 //Other includes
00027 #include <sys/time.h>
00028 #include <iostream>
00029 using namespace std;
00030 
00031 
00032 /*! Constructor */
00033 PerformanceTimer::PerformanceTimer(){
00034         //Timer has not been started yet
00035         timerStopped = true;
00036 
00037         //Initialise the time values to zero
00038         startTime.tv_sec = 0;
00039         startTime.tv_usec = 0;
00040         endTime.tv_sec = 0;
00041         endTime.tv_usec = 0;
00042 }
00043 
00044 
00045 /*! Destructor. */
00046 PerformanceTimer::~PerformanceTimer(){
00047         #ifdef MEMORY_DEBUG
00048                 cout<<"DESTROYING PERFORMANCE TIMER"<<endl;
00049         #endif//MEMORY_DEBUG
00050 }
00051 
00052 
00053 //------------------------------------------------------------------------
00054 //------------------------ PUBLIC METHODS --------------------------------
00055 //------------------------------------------------------------------------
00056 
00057 /*! Returns how long since the timer was started in microseconds. */
00058 unsigned int PerformanceTimer::getTime_usec(){
00059         //Update the current time if the timer is still running
00060         if(!timerStopped)
00061                 gettimeofday(&endTime, NULL);
00062         unsigned int timerTime_us = 1000000 * (endTime.tv_sec - startTime.tv_sec) + endTime.tv_usec - startTime.tv_usec;
00063         return timerTime_us;
00064 }
00065 
00066 
00067 /*! Prints out the time since it started. */
00068 void PerformanceTimer::printTime(){
00069         //Update the current time if the timer is still running
00070         if(!timerStopped)
00071                 gettimeofday(&endTime, NULL);
00072         unsigned int timerTime_us = 1000000 * (endTime.tv_sec - startTime.tv_sec) + endTime.tv_usec - startTime.tv_usec;
00073         cout<<processName<<" took "<<timerTime_us<<" microseconds"<<endl;
00074 }
00075 
00076 
00077 /*! Starts the timer by recording the current time. */
00078 void PerformanceTimer::start(string procName){
00079         processName = procName;
00080         gettimeofday(&startTime, NULL);
00081         gettimeofday(&endTime, NULL);//Also initialise the end time
00082         timerStopped = false;
00083 }
00084 
00085 
00086 /*! Stops the timer. */
00087 void PerformanceTimer::stop(){
00088         gettimeofday(&endTime, NULL);
00089         timerStopped = true;
00090 }
00091 

Generated on Mon Sep 3 22:18:50 2007 for SpikeStream Library by  doxygen 1.4.4