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