mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 1 | #ifndef Stats_DEFINED |
| 2 | #define Stats_DEFINED |
| 3 | |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 4 | #include "SkString.h" |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 5 | #include "SkTSort.h" |
| 6 | |
mtklein | 6238688 | 2014-07-15 10:30:31 -0700 | [diff] [blame] | 7 | #ifdef SK_BUILD_FOR_WIN |
| 8 | static const char* kBars[] = { ".", "o", "O" }; |
| 9 | #else |
| 10 | static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; |
| 11 | #endif |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 12 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 13 | struct Stats { |
| 14 | Stats(const double samples[], int n) { |
| 15 | min = samples[0]; |
| 16 | max = samples[0]; |
| 17 | for (int i = 0; i < n; i++) { |
| 18 | if (samples[i] < min) { min = samples[i]; } |
| 19 | if (samples[i] > max) { max = samples[i]; } |
| 20 | } |
| 21 | |
| 22 | double sum = 0.0; |
| 23 | for (int i = 0 ; i < n; i++) { |
| 24 | sum += samples[i]; |
| 25 | } |
| 26 | mean = sum / n; |
| 27 | |
| 28 | double err = 0.0; |
| 29 | for (int i = 0 ; i < n; i++) { |
| 30 | err += (samples[i] - mean) * (samples[i] - mean); |
| 31 | } |
| 32 | var = err / (n-1); |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 33 | |
| 34 | SkAutoTMalloc<double> sorted(n); |
| 35 | memcpy(sorted.get(), samples, n * sizeof(double)); |
| 36 | SkTQSort(sorted.get(), sorted.get() + n - 1); |
| 37 | median = sorted[n/2]; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 38 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 39 | // 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] | 40 | for (int i = 0; i < n; i++) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 41 | if (min == max) { |
| 42 | // All samples are the same value. Don't divide by zero. |
| 43 | plot.append(kBars[0]); |
| 44 | continue; |
| 45 | } |
| 46 | |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 47 | double s = samples[i]; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 48 | s -= min; |
| 49 | s /= (max - min); |
| 50 | s *= (SK_ARRAY_COUNT(kBars) - 1); |
Mike Klein | 9129477 | 2014-07-16 19:59:32 -0400 | [diff] [blame] | 51 | const size_t bar = (size_t)(s + 0.5); |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 52 | SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars)); |
| 53 | plot.append(kBars[bar]); |
| 54 | } |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | double min; |
| 58 | double max; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 59 | double mean; // Estimate of population mean. |
| 60 | double var; // Estimate of population variance. |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 61 | double median; |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 62 | SkString plot; // A single-line bar chart (_not_ histogram) of the samples. |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #endif//Stats_DEFINED |