mirror of
https://github.com/adulau/aha.git
synced 2024-12-28 11:46:19 +00:00
perf stat: More advanced variance computation
Use the more advanced single pass variance algorithm outlined on the wikipedia page. This is numerically more stable for larger sample sets. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
parent
63d40deb2e
commit
8a02631a47
1 changed files with 12 additions and 12 deletions
|
@ -79,29 +79,30 @@ static int event_scaled[MAX_COUNTERS];
|
||||||
|
|
||||||
struct stats
|
struct stats
|
||||||
{
|
{
|
||||||
double sum;
|
double n, mean, M2;
|
||||||
double sum_sq;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void update_stats(struct stats *stats, u64 val)
|
static void update_stats(struct stats *stats, u64 val)
|
||||||
{
|
{
|
||||||
double sq = val;
|
double delta;
|
||||||
|
|
||||||
stats->sum += val;
|
stats->n++;
|
||||||
stats->sum_sq += sq * sq;
|
delta = val - stats->mean;
|
||||||
|
stats->mean += delta / stats->n;
|
||||||
|
stats->M2 += delta*(val - stats->mean);
|
||||||
}
|
}
|
||||||
|
|
||||||
static double avg_stats(struct stats *stats)
|
static double avg_stats(struct stats *stats)
|
||||||
{
|
{
|
||||||
return stats->sum / run_count;
|
return stats->mean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
|
* http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
|
||||||
*
|
*
|
||||||
* (\Sum n_i^2) - ((\Sum n_i)^2)/n
|
* (\Sum n_i^2) - ((\Sum n_i)^2)/n
|
||||||
* s^2 -------------------------------
|
* s^2 = -------------------------------
|
||||||
* n - 1
|
* n - 1
|
||||||
*
|
*
|
||||||
* http://en.wikipedia.org/wiki/Stddev
|
* http://en.wikipedia.org/wiki/Stddev
|
||||||
*
|
*
|
||||||
|
@ -114,9 +115,8 @@ static double avg_stats(struct stats *stats)
|
||||||
*/
|
*/
|
||||||
static double stddev_stats(struct stats *stats)
|
static double stddev_stats(struct stats *stats)
|
||||||
{
|
{
|
||||||
double avg = stats->sum / run_count;
|
double variance = stats->M2 / (stats->n - 1);
|
||||||
double variance = (stats->sum_sq - stats->sum*avg)/(run_count - 1);
|
double variance_mean = variance / stats->n;
|
||||||
double variance_mean = variance / run_count;
|
|
||||||
|
|
||||||
return sqrt(variance_mean);
|
return sqrt(variance_mean);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue