Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: tools/Stats.h

Issue 390483002: nanobench: add a cute bar chart (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: size_t >= 0, duh Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « bench/nanobench.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #ifndef Stats_DEFINED 1 #ifndef Stats_DEFINED
2 #define Stats_DEFINED 2 #define Stats_DEFINED
3 3
4 #include <math.h>
5
6 #include "SkString.h"
4 #include "SkTSort.h" 7 #include "SkTSort.h"
5 8
9 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
10
6 struct Stats { 11 struct Stats {
7 Stats(const double samples[], int n) { 12 Stats(const double samples[], int n) {
8 min = samples[0]; 13 min = samples[0];
9 max = samples[0]; 14 max = samples[0];
10 for (int i = 0; i < n; i++) { 15 for (int i = 0; i < n; i++) {
11 if (samples[i] < min) { min = samples[i]; } 16 if (samples[i] < min) { min = samples[i]; }
12 if (samples[i] > max) { max = samples[i]; } 17 if (samples[i] > max) { max = samples[i]; }
13 } 18 }
14 19
15 double sum = 0.0; 20 double sum = 0.0;
16 for (int i = 0 ; i < n; i++) { 21 for (int i = 0 ; i < n; i++) {
17 sum += samples[i]; 22 sum += samples[i];
18 } 23 }
19 mean = sum / n; 24 mean = sum / n;
20 25
21 double err = 0.0; 26 double err = 0.0;
22 for (int i = 0 ; i < n; i++) { 27 for (int i = 0 ; i < n; i++) {
23 err += (samples[i] - mean) * (samples[i] - mean); 28 err += (samples[i] - mean) * (samples[i] - mean);
24 } 29 }
25 var = err / (n-1); 30 var = err / (n-1);
26 31
27 SkAutoTMalloc<double> sorted(n); 32 SkAutoTMalloc<double> sorted(n);
28 memcpy(sorted.get(), samples, n * sizeof(double)); 33 memcpy(sorted.get(), samples, n * sizeof(double));
29 SkTQSort(sorted.get(), sorted.get() + n - 1); 34 SkTQSort(sorted.get(), sorted.get() + n - 1);
30 median = sorted[n/2]; 35 median = sorted[n/2];
36
37 for (int i = 0; i < n; i++) {
38 double s = samples[i];
39 // Normalize samples to [min, max] in as many quanta as we have dist inct bars to print.
40 s -= min;
41 s /= (max - min);
42 s *= (SK_ARRAY_COUNT(kBars) - 1);
43 const size_t bar = (size_t)round(s);
44 SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars));
45 plot.append(kBars[bar]);
46 }
31 } 47 }
32 48
33 double min; 49 double min;
34 double max; 50 double max;
35 double mean; // Estimate of population mean. 51 double mean; // Estimate of population mean.
36 double var; // Estimate of population variance. 52 double var; // Estimate of population variance.
37 double median; 53 double median;
54 SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
38 }; 55 };
39 56
40 #endif//Stats_DEFINED 57 #endif//Stats_DEFINED
OLDNEW
« no previous file with comments | « bench/nanobench.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698