skia2/tools/Stats.h
Mike Klein 912947737a Use __rdtsc on Windows.
This seems to be ~100x higher resolution than QueryPerformanceCounter.  AFAIK, all our Windows perf bots have constant_tsc, so we can be a bit more direct about using rdtsc directly: it'll always tick at the max CPU frequency.

Now, the question remains, what is the max CPU frequency to divide through by?  It looks like QueryPerformanceFrequency actually gives the CPU frequency in kHz, suspiciously exactly what we need to divide through to get elapsed milliseconds.  That was a freebie.

I did some before/after comparison on slow benchmarks.  Timings look the same.  Going to land this without review tonight to see what happens on the bots; happy to review carefully tomorrow.

R=mtklein@google.com
TBR=bungeman

BUG=skia:

Review URL: https://codereview.chromium.org/394363003
2014-07-16 19:59:32 -04:00

66 lines
1.9 KiB
C

#ifndef Stats_DEFINED
#define Stats_DEFINED
#include "SkString.h"
#include "SkTSort.h"
#ifdef SK_BUILD_FOR_WIN
static const char* kBars[] = { ".", "o", "O" };
#else
static const char* kBars[] = { "", "", "", "", "", "", "", "" };
#endif
struct Stats {
Stats(const double samples[], int n) {
min = samples[0];
max = samples[0];
for (int i = 0; i < n; i++) {
if (samples[i] < min) { min = samples[i]; }
if (samples[i] > max) { max = samples[i]; }
}
double sum = 0.0;
for (int i = 0 ; i < n; i++) {
sum += samples[i];
}
mean = sum / n;
double err = 0.0;
for (int i = 0 ; i < n; i++) {
err += (samples[i] - mean) * (samples[i] - mean);
}
var = err / (n-1);
SkAutoTMalloc<double> sorted(n);
memcpy(sorted.get(), samples, n * sizeof(double));
SkTQSort(sorted.get(), sorted.get() + n - 1);
median = sorted[n/2];
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
for (int i = 0; i < n; i++) {
if (min == max) {
// All samples are the same value. Don't divide by zero.
plot.append(kBars[0]);
continue;
}
double s = samples[i];
s -= min;
s /= (max - min);
s *= (SK_ARRAY_COUNT(kBars) - 1);
const size_t bar = (size_t)(s + 0.5);
SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars));
plot.append(kBars[bar]);
}
}
double min;
double max;
double mean; // Estimate of population mean.
double var; // Estimate of population variance.
double median;
SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
};
#endif//Stats_DEFINED