2
* vim: ts=4 sw=4 et tw=0 wm=0
4
* libavoid - Fast, Incremental, Object-avoiding Line Router
5
* Copyright (C) 2004-2006 Michael Wybrow <mjwybrow@users.sourceforge.net>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
#include "libavoid/timer.h"
40
void Timer::Reset(void)
42
for (int i = 0; i < tmCount; i++)
45
cTotal[i] = cPath[i] = 0;
46
cTally[i] = cPathTally[i] = 0;
47
cMax[i] = cPathMax[i] = 0;
51
type = lasttype = tmNon;
55
void Timer::Register(const int t, const bool start)
74
void Timer::Start(void)
78
fprintf(stderr, "ERROR: Timer already running in Timer::Start()\n");
81
cStart[type] = clock(); // CPU time
86
void Timer::Stop(void)
90
fprintf(stderr, "ERROR: Timer not running in Timer::Stop()\n");
93
clock_t cStop = clock(); // CPU time
97
if (cStop < cStart[type])
99
// Uh-oh, the clock value has wrapped around.
101
bigclock_t realStop = ((bigclock_t) cStop) + ULONG_MAX + 1;
102
cDiff = realStop - cStart[type];
106
cDiff = cStop - cStart[type];
109
if (cDiff > LONG_MAX)
111
fprintf(stderr, "Error: cDiff overflow in Timer:Stop()\n");
117
cPath[lasttype] += cDiff;
118
cPathTally[lasttype]++;
119
if (((clock_t) cDiff) > cPathMax[lasttype])
121
cPathMax[lasttype] = (clock_t) cDiff;
126
cTotal[type] += cDiff;
128
if (((clock_t) cDiff) > cMax[type])
130
cMax[type] = (clock_t) cDiff;
139
void Timer::PrintAll(void)
141
for (int i = 0; i < tmCount; i++)
148
#define toMsec(tot) ((bigclock_t) ((tot) / (((double) CLOCKS_PER_SEC) / 1000)))
149
#define toAvg(tot, cnt) ((((cnt) > 0) ? ((long double) (tot)) / (cnt) : 0))
151
void Timer::Print(const int t)
153
bigclock_t avg = toMsec(toAvg(cTotal[t], cTally[t]));
154
bigclock_t pind = toMsec(toAvg(cPath[t], cPathTally[t]));
155
bigclock_t pavg = toMsec(toAvg(cPath[t], cTally[t]));
156
double max = toMsec(cMax[t]);
157
double pmax = toMsec(cPathMax[t]);
158
printf("\t%lld %d %lld %.0f %lld %d %lld %.0f %lld\n",
159
cTotal[t], cTally[t], avg, max,
160
cPath[t], cPathTally[t], pavg, pmax, pind);