cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 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 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 8 | #ifndef Stats_DEFINED |
| 9 | #define Stats_DEFINED |
| 10 | |
Mike Klein | a4c277b | 2018-11-06 14:24:55 -0500 | [diff] [blame] | 11 | #include "SkFloatingPoint.h" |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 12 | #include "SkString.h" |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 13 | #include "SkTSort.h" |
| 14 | |
mtklein | 6238688 | 2014-07-15 10:30:31 -0700 | [diff] [blame] | 15 | #ifdef SK_BUILD_FOR_WIN |
| 16 | static const char* kBars[] = { ".", "o", "O" }; |
| 17 | #else |
| 18 | static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; |
| 19 | #endif |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 20 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 21 | struct Stats { |
Mike Klein | 512a244 | 2018-09-27 10:44:56 -0400 | [diff] [blame] | 22 | Stats(const SkTArray<double>& samples, bool want_plot) { |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 23 | int n = samples.count(); |
| 24 | if (!n) { |
| 25 | min = max = mean = var = median = 0; |
| 26 | return; |
| 27 | } |
| 28 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 29 | min = samples[0]; |
| 30 | max = samples[0]; |
| 31 | for (int i = 0; i < n; i++) { |
| 32 | if (samples[i] < min) { min = samples[i]; } |
| 33 | if (samples[i] > max) { max = samples[i]; } |
| 34 | } |
| 35 | |
| 36 | double sum = 0.0; |
| 37 | for (int i = 0 ; i < n; i++) { |
| 38 | sum += samples[i]; |
| 39 | } |
| 40 | mean = sum / n; |
| 41 | |
| 42 | double err = 0.0; |
| 43 | for (int i = 0 ; i < n; i++) { |
| 44 | err += (samples[i] - mean) * (samples[i] - mean); |
| 45 | } |
Mike Klein | a4c277b | 2018-11-06 14:24:55 -0500 | [diff] [blame] | 46 | var = sk_ieee_double_divide(err, n-1); |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 47 | |
| 48 | SkAutoTMalloc<double> sorted(n); |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 49 | memcpy(sorted.get(), samples.begin(), n * sizeof(double)); |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 50 | SkTQSort(sorted.get(), sorted.get() + n - 1); |
| 51 | median = sorted[n/2]; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 52 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 53 | // Normalize samples to [min, max] in as many quanta as we have distinct bars to print. |
Mike Klein | 512a244 | 2018-09-27 10:44:56 -0400 | [diff] [blame] | 54 | for (int i = 0; want_plot && i < n; i++) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 55 | if (min == max) { |
| 56 | // All samples are the same value. Don't divide by zero. |
| 57 | plot.append(kBars[0]); |
| 58 | continue; |
| 59 | } |
| 60 | |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 61 | double s = samples[i]; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 62 | s -= min; |
| 63 | s /= (max - min); |
| 64 | s *= (SK_ARRAY_COUNT(kBars) - 1); |
Mike Klein | 9129477 | 2014-07-16 19:59:32 -0400 | [diff] [blame] | 65 | const size_t bar = (size_t)(s + 0.5); |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 66 | SkASSERT_RELEASE(bar < SK_ARRAY_COUNT(kBars)); |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 67 | plot.append(kBars[bar]); |
| 68 | } |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | double min; |
| 72 | double max; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 73 | double mean; // Estimate of population mean. |
| 74 | double var; // Estimate of population variance. |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 75 | double median; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 76 | SkString plot; // A single-line bar chart (_not_ histogram) of the samples. |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #endif//Stats_DEFINED |