| 1 |
/* |
| 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 |
|
| 8 |
#ifndef Stats_DEFINED |
| 9 |
#define Stats_DEFINED |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include "include/core/SkString.h" |
| 15 |
#include "include/private/base/SkFloatingPoint.h" |
| 16 |
|
| 17 |
#ifdef SK_BUILD_FOR_WIN |
| 18 |
static const char* kBars[] = { ".", "o", "O" }; |
| 19 |
#else |
| 20 |
static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; |
| 21 |
#endif |
| 22 |
|
| 23 |
struct Stats { |
| 24 |
Stats(const skia_private::TArray<double>& samples, bool want_plot) { |
| 25 |
int n = samples.size(); |
| 26 |
if (!n) { |
| 27 |
min = max = mean = var = median = 0; |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
min = samples[0]; |
| 32 |
max = samples[0]; |
| 33 |
for (int i = 0; i < n; i++) { |
| 34 |
if (samples[i] < min) { min = samples[i]; } |
| 35 |
if (samples[i] > max) { max = samples[i]; } |
| 36 |
} |
| 37 |
|
| 38 |
double sum = 0.0; |
| 39 |
for (int i = 0 ; i < n; i++) { |
| 40 |
sum += samples[i]; |
| 41 |
} |
| 42 |
mean = sum / n; |
| 43 |
|
| 44 |
double err = 0.0; |
| 45 |
for (int i = 0 ; i < n; i++) { |
| 46 |
err += (samples[i] - mean) * (samples[i] - mean); |
| 47 |
} |
| 48 |
var = sk_ieee_double_divide(err, n-1); |
| 49 |
|
| 50 |
std::vector<double> sorted(samples.begin(), samples.end()); |
| 51 |
std::sort(sorted.begin(), sorted.end()); |
| 52 |
median = sorted[n/2]; |
| 53 |
|
| 54 |
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print. |
| 55 |
for (int i = 0; want_plot && i < n; i++) { |
| 56 |
if (min == max) { |
| 57 |
// All samples are the same value. Don't divide by zero. |
| 58 |
plot.append(kBars[0]); |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
double s = samples[i]; |
| 63 |
s -= min; |
| 64 |
s /= (max - min); |
| 65 |
s *= (std::size(kBars) - 1); |
| 66 |
const size_t bar = (size_t)(s + 0.5); |
| 67 |
SkASSERT_RELEASE(bar < std::size(kBars)); |
| 68 |
plot.append(kBars[bar]); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
double min; |
| 73 |
double max; |
| 74 |
double mean; // Estimate of population mean. |
| 75 |
double var; // Estimate of population variance. |
| 76 |
double median; |
| 77 |
SkString plot; // A single-line bar chart (_not_ histogram) of the samples. |
| 78 |
}; |
| 79 |
|
| 80 |
#endif//Stats_DEFINED |