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