2015-06-26 02:17:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-06-16 21:04:32 +00:00
|
|
|
#ifndef Stats_DEFINED
|
|
|
|
#define Stats_DEFINED
|
|
|
|
|
2014-07-11 18:57:07 +00:00
|
|
|
#include "SkString.h"
|
2014-07-09 15:46:49 +00:00
|
|
|
#include "SkTSort.h"
|
|
|
|
|
2014-07-15 17:30:31 +00:00
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
static const char* kBars[] = { ".", "o", "O" };
|
|
|
|
#else
|
|
|
|
static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
|
|
|
|
#endif
|
2014-07-11 18:57:07 +00:00
|
|
|
|
2014-06-16 21:04:32 +00:00
|
|
|
struct Stats {
|
2015-06-26 02:17:08 +00:00
|
|
|
Stats(const SkTArray<double>& samples) {
|
|
|
|
int n = samples.count();
|
|
|
|
if (!n) {
|
|
|
|
min = max = mean = var = median = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-16 21:04:32 +00:00
|
|
|
min = samples[0];
|
|
|
|
max = samples[0];
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
if (samples[i] < min) { min = samples[i]; }
|
|
|
|
if (samples[i] > max) { max = samples[i]; }
|
|
|
|
}
|
|
|
|
|
|
|
|
double sum = 0.0;
|
|
|
|
for (int i = 0 ; i < n; i++) {
|
|
|
|
sum += samples[i];
|
|
|
|
}
|
|
|
|
mean = sum / n;
|
|
|
|
|
|
|
|
double err = 0.0;
|
|
|
|
for (int i = 0 ; i < n; i++) {
|
|
|
|
err += (samples[i] - mean) * (samples[i] - mean);
|
|
|
|
}
|
|
|
|
var = err / (n-1);
|
2014-07-09 15:46:49 +00:00
|
|
|
|
|
|
|
SkAutoTMalloc<double> sorted(n);
|
2015-06-26 02:17:08 +00:00
|
|
|
memcpy(sorted.get(), samples.begin(), n * sizeof(double));
|
2014-07-09 15:46:49 +00:00
|
|
|
SkTQSort(sorted.get(), sorted.get() + n - 1);
|
|
|
|
median = sorted[n/2];
|
2014-07-11 18:57:07 +00:00
|
|
|
|
2014-07-14 19:28:47 +00:00
|
|
|
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
|
2014-07-11 18:57:07 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
2014-07-14 19:28:47 +00:00
|
|
|
if (min == max) {
|
|
|
|
// All samples are the same value. Don't divide by zero.
|
|
|
|
plot.append(kBars[0]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-11 18:57:07 +00:00
|
|
|
double s = samples[i];
|
|
|
|
s -= min;
|
|
|
|
s /= (max - min);
|
|
|
|
s *= (SK_ARRAY_COUNT(kBars) - 1);
|
2014-07-16 23:59:32 +00:00
|
|
|
const size_t bar = (size_t)(s + 0.5);
|
2016-01-22 14:50:25 +00:00
|
|
|
SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars));
|
2014-07-11 18:57:07 +00:00
|
|
|
plot.append(kBars[bar]);
|
|
|
|
}
|
2014-06-16 21:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double min;
|
|
|
|
double max;
|
2014-07-11 18:57:07 +00:00
|
|
|
double mean; // Estimate of population mean.
|
|
|
|
double var; // Estimate of population variance.
|
2014-07-09 15:46:49 +00:00
|
|
|
double median;
|
2014-07-11 18:57:07 +00:00
|
|
|
SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
|
2014-06-16 21:04:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif//Stats_DEFINED
|