blob: 8487a9497da6dff23cf2508dad5831065deeaa29 [file] [log] [blame]
mtklein90c471e2014-06-16 14:04:32 -07001#ifndef Stats_DEFINED
2#define Stats_DEFINED
3
mtklein5d9d10e2014-07-11 11:57:07 -07004#include "SkString.h"
mtklein40b32be2014-07-09 08:46:49 -07005#include "SkTSort.h"
6
mtklein62386882014-07-15 10:30:31 -07007#ifdef SK_BUILD_FOR_WIN
8 static const char* kBars[] = { ".", "o", "O" };
9#else
10 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
11#endif
mtklein5d9d10e2014-07-11 11:57:07 -070012
mtklein90c471e2014-06-16 14:04:32 -070013struct Stats {
14 Stats(const double samples[], int n) {
15 min = samples[0];
16 max = samples[0];
17 for (int i = 0; i < n; i++) {
18 if (samples[i] < min) { min = samples[i]; }
19 if (samples[i] > max) { max = samples[i]; }
20 }
21
22 double sum = 0.0;
23 for (int i = 0 ; i < n; i++) {
24 sum += samples[i];
25 }
26 mean = sum / n;
27
28 double err = 0.0;
29 for (int i = 0 ; i < n; i++) {
30 err += (samples[i] - mean) * (samples[i] - mean);
31 }
32 var = err / (n-1);
mtklein40b32be2014-07-09 08:46:49 -070033
34 SkAutoTMalloc<double> sorted(n);
35 memcpy(sorted.get(), samples, n * sizeof(double));
36 SkTQSort(sorted.get(), sorted.get() + n - 1);
37 median = sorted[n/2];
mtklein5d9d10e2014-07-11 11:57:07 -070038
mtkleina189ccd2014-07-14 12:28:47 -070039 // Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
mtklein5d9d10e2014-07-11 11:57:07 -070040 for (int i = 0; i < n; i++) {
mtkleina189ccd2014-07-14 12:28:47 -070041 if (min == max) {
42 // All samples are the same value. Don't divide by zero.
43 plot.append(kBars[0]);
44 continue;
45 }
46
mtklein5d9d10e2014-07-11 11:57:07 -070047 double s = samples[i];
mtklein5d9d10e2014-07-11 11:57:07 -070048 s -= min;
49 s /= (max - min);
50 s *= (SK_ARRAY_COUNT(kBars) - 1);
Mike Klein91294772014-07-16 19:59:32 -040051 const size_t bar = (size_t)(s + 0.5);
mtklein5d9d10e2014-07-11 11:57:07 -070052 SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars));
53 plot.append(kBars[bar]);
54 }
mtklein90c471e2014-06-16 14:04:32 -070055 }
56
57 double min;
58 double max;
mtklein5d9d10e2014-07-11 11:57:07 -070059 double mean; // Estimate of population mean.
60 double var; // Estimate of population variance.
mtklein40b32be2014-07-09 08:46:49 -070061 double median;
mtklein5d9d10e2014-07-11 11:57:07 -070062 SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
mtklein90c471e2014-06-16 14:04:32 -070063};
64
65#endif//Stats_DEFINED