Found my online stats calc code. It's in C but I can rework it. This should work better for FPS calc.
This commit is contained in:
parent
7a74877849
commit
e3e0f0a322
2 changed files with 90 additions and 0 deletions
65
stats.c
Normal file
65
stats.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <math.h>
|
||||
#include <lcthw/stats.h>
|
||||
#include <stdlib.h>
|
||||
#include <lcthw/dbg.h>
|
||||
|
||||
Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
|
||||
double min, double max)
|
||||
{
|
||||
Stats *st = malloc(sizeof(Stats));
|
||||
check_mem(st);
|
||||
|
||||
st->sum = sum;
|
||||
st->sumsq = sumsq;
|
||||
st->n = n;
|
||||
st->min = min;
|
||||
st->max = max;
|
||||
|
||||
return st;
|
||||
|
||||
error:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Stats *Stats_create()
|
||||
{
|
||||
return Stats_recreate(0.0, 0.0, 0L, 0.0, 0.0);
|
||||
}
|
||||
|
||||
double Stats_mean(Stats * st)
|
||||
{
|
||||
return st->sum / st->n;
|
||||
}
|
||||
|
||||
double Stats_stddev(Stats * st)
|
||||
{
|
||||
return sqrt((st->sumsq - (st->sum * st->sum / st->n)) /
|
||||
(st->n - 1));
|
||||
}
|
||||
|
||||
void Stats_sample(Stats * st, double s)
|
||||
{
|
||||
st->sum += s;
|
||||
st->sumsq += s * s;
|
||||
|
||||
if (st->n == 0) {
|
||||
st->min = s;
|
||||
st->max = s;
|
||||
} else {
|
||||
if (st->min > s)
|
||||
st->min = s;
|
||||
if (st->max < s)
|
||||
st->max = s;
|
||||
}
|
||||
|
||||
st->n += 1;
|
||||
}
|
||||
|
||||
void Stats_dump(Stats * st)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"sum: %f, sumsq: %f, n: %ld, "
|
||||
"min: %f, max: %f, mean: %f, stddev: %f",
|
||||
st->sum, st->sumsq, st->n, st->min, st->max, Stats_mean(st),
|
||||
Stats_stddev(st));
|
||||
}
|
25
stats.h
Normal file
25
stats.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef lcthw_stats_h
|
||||
#define lcthw_stats_h
|
||||
|
||||
typedef struct Stats {
|
||||
double sum;
|
||||
double sumsq;
|
||||
unsigned long n;
|
||||
double min;
|
||||
double max;
|
||||
} Stats;
|
||||
|
||||
Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
|
||||
double min, double max);
|
||||
|
||||
Stats *Stats_create();
|
||||
|
||||
double Stats_mean(Stats * st);
|
||||
|
||||
double Stats_stddev(Stats * st);
|
||||
|
||||
void Stats_sample(Stats * st, double s);
|
||||
|
||||
void Stats_dump(Stats * st);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue