Module: rprofile.cpp

class RProfile
.h

constructorRProfile()
destructor~RProfile()
Resetvoid Reset()
Updatevoid Update()

Look at timer and update the current figures

SwitchTovoid SwitchTo(int newPart)

Switch to other part to time

GetTimeForint GetTimeFor(int part)

Returns time in milliseconds for part 'part'
Call Update() BEFORE calling this function (not done here for efficiency
of retrieving profile numbers)

GetTimePercentageForint GetTimePercentageFor(int part)

Convenience function to return the time spent in the part as a percentage
Call Update() BEFORE calling this function (not done here for efficiency
of retrieving profile numbers)



/*
 * RProfile - profiling the code
 * 22-10-00: Created!
 * (c) Dolphinity/Ruud van Gaal
 */

#include <racer/racer.h>
#include <qlib/debug.h>
#pragma hdrstop
DEBUG_ENABLE

RProfile::RProfile()
{
  int i;

  for(i=0;i<PROF_MAX;i++)
    timeUsed[i]=0;
  part=PROF_OTHER;
  tmr=new QTimer();
  tmr->Start();
  totalTime=0;
}

RProfile::~RProfile()
{
  if(tmr)delete tmr;
}

void RProfile::Reset()
{
  int i;

  tmr->Reset();
  totalTime=0;
  for(i=0;i<PROF_MAX;i++)
    timeUsed[i]=0;
  tmr->Start();
}

void RProfile::Update()
// Look at timer and update the current figures
{
  int i,t,newTotalTime;

  newTotalTime=tmr->GetMilliSeconds();

  // Auto reset to keep timing current
  if(newTotalTime>1000)
  {
    // Reset timing counts
    for(i=0;i<PROF_MAX;i++)
      timeUsed[i]=0;
    totalTime=0;
    tmr->Reset();
    tmr->Start();
    return;
  }

  // Get time spent
  t=newTotalTime-totalTime;
  totalTime=newTotalTime;

  // Add to current part
  timeUsed[part]+=t;
}

void RProfile::SwitchTo(int newPart)
// Switch to other part to time
{
  Update();
  // Set new part
  part=newPart;
}

int RProfile::GetTimeFor(int part)
// Returns time in milliseconds for part 'part'
// Call Update() BEFORE calling this function (not done here for efficiency
// of retrieving profile numbers)
{
  return timeUsed[part];
}

int RProfile::GetTimePercentageFor(int part)
// Convenience function to return the time spent in the part as a percentage
// Call Update() BEFORE calling this function (not done here for efficiency
// of retrieving profile numbers)
{
  if(!totalTime)return 0;
//qdbg("timeUsed[%d]=%d, totalTime=%d\n",part,timeUsed[part],totalTime);
  return timeUsed[part]*100/totalTime;
}