1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "timer.h"
#include "log.h"
#include "iostream"
#include <time.h>
#include <assert.h>
using namespace std;
Timer* Timer::timers;
//--------------------------------------------------
// Timer
//--------------------------------------------------
bool Timer::doTimingThisTime()
{
// return false; //Disabling timer
if(n>=skip)n=0;
return n==0;
}
Timer::Timer(const char* s, int skip):
ons(0),
n(0),
total(0)
{
this->s=s;
this->skip=skip;
next=timers;
timers=this;
}
void Timer::on()
{
if(ons!=0)
{
static bool a;
log1 if(!a)cerr<<"Timings are unreliable due to nested timers."<<endl;
a=true;
}
// assert(ons==0);
ons++;
n++;
if(doTimingThisTime())
{
timerStartSec=time(0);
timerStart=clock();
}
}
void Timer::off()
{
if(doTimingThisTime())
{
int t2=time(0);
if((t2-timerStartSec)>(1000))
total+= (t2-timerStartSec);
else
total+=((((double)clock()-(double)timerStart))*((1.0)/CLOCKS_PER_SEC));
}
ons--;
assert(ons>=0);
}
void Timer::print(FILE *f)
{
if(total!=0)
fprintf(f,"%24s %8.2f s\n",s,(float)(total*skip));
}
void Timer::printList(FILE *f)
{
fprintf(f,"Printing timer list:\n");
Timer *l=timers;
while(l)
{
l->print(f);
l=l->next;
}
}
|