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
|
|
|
|
|
2020-07-14 20:28:33 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkFloatingPoint.h"
|
2014-07-09 15:46:49 +00:00
|
|
|
|
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 {
|
2018-09-27 14:44:56 +00:00
|
|
|
Stats(const SkTArray<double>& samples, bool want_plot) {
|
2015-06-26 02:17:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-11-06 19:24:55 +00:00
|
|
|
var = sk_ieee_double_divide(err, n-1);
|
2014-07-09 15:46:49 +00:00
|
|
|
|
2020-07-14 20:28:33 +00:00
|
|
|
std::vector<double> sorted(samples.begin(), samples.end());
|
|
|
|
std::sort(sorted.begin(), sorted.end());
|
2014-07-09 15:46:49 +00:00
|
|
|
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.
|
2018-09-27 14:44:56 +00:00
|
|
|
for (int i = 0; want_plot && 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-29 16:51:04 +00:00
|
|
|
SkASSERT_RELEASE(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
|