blob: 557a2a51a2c61e16e9557d8d20b55f1419c1ea61 [file] [log] [blame]
cdaltone1b89582015-06-25 19:17:08 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein90c471e2014-06-16 14:04:32 -07008#ifndef Stats_DEFINED
9#define Stats_DEFINED
10
mtklein5d9d10e2014-07-11 11:57:07 -070011#include "SkString.h"
mtklein40b32be2014-07-09 08:46:49 -070012#include "SkTSort.h"
13
mtklein62386882014-07-15 10:30:31 -070014#ifdef SK_BUILD_FOR_WIN
15 static const char* kBars[] = { ".", "o", "O" };
16#else
17 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
18#endif
mtklein5d9d10e2014-07-11 11:57:07 -070019
mtklein90c471e2014-06-16 14:04:32 -070020struct Stats {
cdaltone1b89582015-06-25 19:17:08 -070021 Stats(const SkTArray<double>& samples) {
22 int n = samples.count();
23 if (!n) {
24 min = max = mean = var = median = 0;
25 return;
26 }
27
mtklein90c471e2014-06-16 14:04:32 -070028 min = samples[0];
29 max = samples[0];
30 for (int i = 0; i < n; i++) {
31 if (samples[i] < min) { min = samples[i]; }
32 if (samples[i] > max) { max = samples[i]; }
33 }
34
35 double sum = 0.0;
36 for (int i = 0 ; i < n; i++) {
37 sum += samples[i];
38 }
39 mean = sum / n;
40
41 double err = 0.0;
42 for (int i = 0 ; i < n; i++) {
43 err += (samples[i] - mean) * (samples[i] - mean);
44 }
45 var = err / (n-1);
mtklein40b32be2014-07-09 08:46:49 -070046
47 SkAutoTMalloc<double> sorted(n);
cdaltone1b89582015-06-25 19:17:08 -070048 memcpy(sorted.get(), samples.begin(), n * sizeof(double));
mtklein40b32be2014-07-09 08:46:49 -070049 SkTQSort(sorted.get(), sorted.get() + n - 1);
50 median = sorted[n/2];
mtklein5d9d10e2014-07-11 11:57:07 -070051
mtkleina189ccd2014-07-14 12:28:47 -070052 // Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
mtklein5d9d10e2014-07-11 11:57:07 -070053 for (int i = 0; i < n; i++) {
mtkleina189ccd2014-07-14 12:28:47 -070054 if (min == max) {
55 // All samples are the same value. Don't divide by zero.
56 plot.append(kBars[0]);
57 continue;
58 }
59
mtklein5d9d10e2014-07-11 11:57:07 -070060 double s = samples[i];
mtklein5d9d10e2014-07-11 11:57:07 -070061 s -= min;
62 s /= (max - min);
63 s *= (SK_ARRAY_COUNT(kBars) - 1);
Mike Klein91294772014-07-16 19:59:32 -040064 const size_t bar = (size_t)(s + 0.5);
djsollenf2b340f2016-01-29 08:51:04 -080065 SkASSERT_RELEASE(bar < SK_ARRAY_COUNT(kBars));
mtklein5d9d10e2014-07-11 11:57:07 -070066 plot.append(kBars[bar]);
67 }
mtklein90c471e2014-06-16 14:04:32 -070068 }
69
70 double min;
71 double max;
mtklein5d9d10e2014-07-11 11:57:07 -070072 double mean; // Estimate of population mean.
73 double var; // Estimate of population variance.
mtklein40b32be2014-07-09 08:46:49 -070074 double median;
mtklein5d9d10e2014-07-11 11:57:07 -070075 SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
mtklein90c471e2014-06-16 14:04:32 -070076};
77
78#endif//Stats_DEFINED