Debian Codegraph @

xo/stats.cxx

raw
1 /* stats.cxx
2 $Id: stats.cxx,v 1.4 2000/01/13 07:04:46 elf Exp $
3
4 written by Marc Singer
5 3 October 1996
6
7 This file is part of the project XO. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27
28 Support for (simple) statistical analysis of program performance
29 data.
30
31 */
32
33 /* ----- Includes */
34
35 #include "standard.h"
36 #include "stats.h"
37 #include "math.h"
38 #include "sys/time.h"
39 #include <assert.h>
40
41
42 /* ----- Class Globals/Statics */
43
44 /* ----- Methods */
45
46
47 void LStatistics::calc (float& rMean, float& rStddev, long& min, long& max)
48 {
49 double sum_xx = 0.0;
50 double sum_x = 0.0;
51 min = LONG_MAX;
52 max = LONG_MIN;
53
54 // TraceMsg (NULL, "stddev %d samples", count ());
55
56 for (int c = count (); c--; ) {
57 sum_xx += (double) m_rgSamples[c]*(double) m_rgSamples[c];
58 sum_x += (double) m_rgSamples[c];
59 if (m_rgSamples[c] > max)
60 max = m_rgSamples[c];
61 if (m_rgSamples[c] < min)
62 min = m_rgSamples[c];
63 }
64
65 double n = (double) count ();
66 rMean = float (sum_x/n);
67 rStddev = float (sqrt ((sum_xx - (sum_x*sum_x)/n)/(n - 1.0)));
68 }
69
70
71
72 float LStatistics::mean (void)
73 {
74 // TraceMsg (NULL, "mean has %d samples", count ());
75 double value = 0.0;
76 for (int c = count (); c--; )
77 value += (double) m_rgSamples[c];
78 return float (value/(double) count ());
79 }
80
81
82 /* LStatistics::sample
83
84 collects a sample. It save the last
85 sizeof (m_rgSamples)/sizeof (long)
86 samples.
87
88 */
89
90 void LStatistics::sample (long sample)
91 {
92 int i = int (((m_cSamples++) % (sizeof (m_rgSamples)/sizeof (long))));
93 assert (i < C_STAT_SAMPLES);
94 m_rgSamples[i] = sample;
95 }
96
97
98 /* LStatistics::stddev
99
100 calculates the sample standard deviation where the samples
101 represent a subset of a larger population. The compuation has been
102 simplified algebraicly from the definition of the standard
103 deviation.
104
105 */
106
107 float LStatistics::stddev (void)
108 {
109 double sum_xx = 0.0;
110 double sum_x = 0.0;
111 double n = (double) count ();
112
113 // TraceMsg (NULL, "stddev %d samples", count ());
114
115 for (int c = count (); c--; ) {
116 sum_xx += (double) m_rgSamples[c]*(double) m_rgSamples[c];
117 sum_x += (double) m_rgSamples[c];
118 }
119 return float (sqrt ((sum_xx - (sum_x*sum_x)/n)/(n - 1.0)));
120 }
121
122 long LTime::delta (void)
123 {
124 // struct tms l_times;
125 // struct rusage l_usage;
126 struct timeval l_timeval;
127
128 // times (&l_times);
129
130 // getrusage (RUSAGE_SELF, &l_usage);
131 // return (l_usage.ru_utime.tv_sec - m_usage.ru_utime.tv_sec)*1000000L
132 // + (l_usage.ru_utime.tv_usec - m_usage.ru_utime.tv_usec);
133
134 gettimeofday (&l_timeval, NULL);
135 return (l_timeval.tv_sec - m_timeval.tv_sec)*1000000L
136 + (l_timeval.tv_usec - m_timeval.tv_usec);
137 }
138
139
140 void LTime::reset (void)
141 {
142 // times (&m_times);
143 // getrusage (RUSAGE_SELF, &m_usage);
144 gettimeofday (&m_timeval, NULL);
145 }