Code

Missed a win32 fix.
[inkscape.git] / src / libavoid / timer.cpp
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libavoid - Fast, Incremental, Object-avoiding Line Router
5  *
6  * Copyright (C) 2004-2008  Monash University
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  * See the file LICENSE.LGPL distributed with the library.
13  *
14  * Licensees holding a valid commercial license may use this file in
15  * accordance with the commercial license agreement provided with the 
16  * library.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
21  *
22  * Author(s):   Michael Wybrow <mjwybrow@users.sourceforge.net>
23 */
26 #include <cstdio>
27 #include <cstdlib>
28 #include <climits>
30 #include "libavoid/timer.h"
31 #include "libavoid/debug.h"
32 #include "libavoid/assertions.h"
34 namespace Avoid {
37 Timer::Timer()
38 {
39     Reset();
40 }
43 void Timer::Reset(void)
44 {
45     for (int i = 0; i < tmCount; i++)
46     {
47         //tTotal[i] = 0;
48         cTotal[i] = cPath[i] = 0;
49         cTally[i] = cPathTally[i] = 0;
50         cMax[i] = cPathMax[i] = 0;
51     }
52     running = false;
53     count  = 0;
54     type = lasttype = tmNon;
55 }
58 void Timer::Register(const TimerIndex t, const bool start)
59 {
60     COLA_ASSERT(t != tmNon);
62     if (type == tmNon)
63     {
64         type = t;
65     }
66     else
67     {
68         type = tmSev;
69     }
71     if (start)
72     {
73         Start();
74     }
75 }
77 void Timer::Start(void)
78 {
79     COLA_ASSERT(!running);
80     cStart[type] = clock();  // CPU time
81     running = true;
82 }
85 void Timer::Stop(void)
86 {
87     COLA_ASSERT(running);
88     clock_t cStop = clock();      // CPU time
89     running = false;
91     bigclock_t cDiff;
92     if (cStop < cStart[type])
93     {
94         // Uh-oh, the clock value has wrapped around.
95         //
96         bigclock_t realStop = ((bigclock_t) cStop) + ULONG_MAX + 1;
97         cDiff = realStop - cStart[type];
98     }
99     else
100     {
101         cDiff = cStop - cStart[type];
102     }
103     
104     COLA_ASSERT(cDiff < LONG_MAX);
106     if (type == tmPth)
107     {
108         cPath[lasttype] += cDiff;
109         cPathTally[lasttype]++;
110         if (((clock_t) cDiff) > cPathMax[lasttype])
111         {
112             cPathMax[lasttype] = (clock_t) cDiff;
113         }
114     }
115     else
116     {
117         cTotal[type] += cDiff;
118         cTally[type]++;
119         if (((clock_t) cDiff) > cMax[type])
120         {
121             cMax[type] = (clock_t) cDiff;
122         }
123         lasttype = type;
124     }
126     type = tmNon;
130 void Timer::PrintAll(FILE *fp)
132     for (unsigned int i = 0; i < tmCount; i++)
133     {
134         Print((TimerIndex) i, fp);
135     }
139 #define toMsec(tot) ((bigclock_t) ((tot) / (((double) CLOCKS_PER_SEC) / 1000)))
140 #define toAvg(tot, cnt) ((((cnt) > 0) ? ((long double) (tot)) / (cnt) : 0))
142 void Timer::Print(const TimerIndex t, FILE *fp)
144    bigclock_t avg = toMsec(toAvg(cTotal[t], cTally[t]));
145    bigclock_t pind = toMsec(toAvg(cPath[t], cPathTally[t]));
146    bigclock_t pavg = toMsec(toAvg(cPath[t], cTally[t]));
147    double max = toMsec(cMax[t]); 
148    double pmax = toMsec(cPathMax[t]);
149    fprintf(fp, "\t%lld %d %lld %.0f %lld %d %lld %.0f %lld\n",
150            cTotal[t], cTally[t], avg, max,
151            cPath[t], cPathTally[t], pavg, pmax, pind);