OLD | NEW |
| 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 |
1 #ifndef Stats_DEFINED | 8 #ifndef Stats_DEFINED |
2 #define Stats_DEFINED | 9 #define Stats_DEFINED |
3 | 10 |
4 #include "SkString.h" | 11 #include "SkString.h" |
5 #include "SkTSort.h" | 12 #include "SkTSort.h" |
6 | 13 |
7 #ifdef SK_BUILD_FOR_WIN | 14 #ifdef SK_BUILD_FOR_WIN |
8 static const char* kBars[] = { ".", "o", "O" }; | 15 static const char* kBars[] = { ".", "o", "O" }; |
9 #else | 16 #else |
10 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; | 17 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; |
11 #endif | 18 #endif |
12 | 19 |
13 struct Stats { | 20 struct Stats { |
14 Stats(const double samples[], int n) { | 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 |
15 min = samples[0]; | 28 min = samples[0]; |
16 max = samples[0]; | 29 max = samples[0]; |
17 for (int i = 0; i < n; i++) { | 30 for (int i = 0; i < n; i++) { |
18 if (samples[i] < min) { min = samples[i]; } | 31 if (samples[i] < min) { min = samples[i]; } |
19 if (samples[i] > max) { max = samples[i]; } | 32 if (samples[i] > max) { max = samples[i]; } |
20 } | 33 } |
21 | 34 |
22 double sum = 0.0; | 35 double sum = 0.0; |
23 for (int i = 0 ; i < n; i++) { | 36 for (int i = 0 ; i < n; i++) { |
24 sum += samples[i]; | 37 sum += samples[i]; |
25 } | 38 } |
26 mean = sum / n; | 39 mean = sum / n; |
27 | 40 |
28 double err = 0.0; | 41 double err = 0.0; |
29 for (int i = 0 ; i < n; i++) { | 42 for (int i = 0 ; i < n; i++) { |
30 err += (samples[i] - mean) * (samples[i] - mean); | 43 err += (samples[i] - mean) * (samples[i] - mean); |
31 } | 44 } |
32 var = err / (n-1); | 45 var = err / (n-1); |
33 | 46 |
34 SkAutoTMalloc<double> sorted(n); | 47 SkAutoTMalloc<double> sorted(n); |
35 memcpy(sorted.get(), samples, n * sizeof(double)); | 48 memcpy(sorted.get(), samples.begin(), n * sizeof(double)); |
36 SkTQSort(sorted.get(), sorted.get() + n - 1); | 49 SkTQSort(sorted.get(), sorted.get() + n - 1); |
37 median = sorted[n/2]; | 50 median = sorted[n/2]; |
38 | 51 |
39 // Normalize samples to [min, max] in as many quanta as we have distinct
bars to print. | 52 // Normalize samples to [min, max] in as many quanta as we have distinct
bars to print. |
40 for (int i = 0; i < n; i++) { | 53 for (int i = 0; i < n; i++) { |
41 if (min == max) { | 54 if (min == max) { |
42 // All samples are the same value. Don't divide by zero. | 55 // All samples are the same value. Don't divide by zero. |
43 plot.append(kBars[0]); | 56 plot.append(kBars[0]); |
44 continue; | 57 continue; |
45 } | 58 } |
(...skipping 10 matching lines...) Expand all Loading... |
56 | 69 |
57 double min; | 70 double min; |
58 double max; | 71 double max; |
59 double mean; // Estimate of population mean. | 72 double mean; // Estimate of population mean. |
60 double var; // Estimate of population variance. | 73 double var; // Estimate of population variance. |
61 double median; | 74 double median; |
62 SkString plot; // A single-line bar chart (_not_ histogram) of the samples. | 75 SkString plot; // A single-line bar chart (_not_ histogram) of the samples. |
63 }; | 76 }; |
64 | 77 |
65 #endif//Stats_DEFINED | 78 #endif//Stats_DEFINED |
OLD | NEW |