Add rolling stddev
parent
5fb61e827a
commit
7b415a8a8e
|
@ -0,0 +1,4 @@
|
||||||
|
test: test_clock
|
||||||
|
./test_clock
|
||||||
|
test_clock:
|
||||||
|
gcc -O3 -o test_clock test_clock.c -lm
|
32
test_clock.c
32
test_clock.c
|
@ -1,17 +1,43 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
#include "x86intrin.h"
|
#include "x86intrin.h"
|
||||||
|
|
||||||
|
struct rolling_stddev
|
||||||
|
{
|
||||||
|
uint64_t sum, count;
|
||||||
|
float mean, m2;
|
||||||
|
};
|
||||||
|
|
||||||
|
void roll(struct rolling_stddev *r, uint64_t value)
|
||||||
|
{
|
||||||
|
r->count++;
|
||||||
|
r->sum += value;
|
||||||
|
float d = value-r->mean;
|
||||||
|
r->mean += d/r->count;
|
||||||
|
r->m2 += d*(value-r->mean);
|
||||||
|
}
|
||||||
|
|
||||||
|
float dev(struct rolling_stddev *r)
|
||||||
|
{
|
||||||
|
return (r->m2/(r->count-1));
|
||||||
|
}
|
||||||
|
|
||||||
void test_clock(const char *name, clockid_t clockid)
|
void test_clock(const char *name, clockid_t clockid)
|
||||||
{
|
{
|
||||||
struct timespec tp, te;
|
struct rolling_stddev d = { 0 };
|
||||||
clock_gettime(clockid, &te);
|
struct timespec ts, tp, te;
|
||||||
|
clock_gettime(clockid, &ts);
|
||||||
|
te = ts;
|
||||||
for (int i = 0; i < 100000000; i++)
|
for (int i = 0; i < 100000000; i++)
|
||||||
{
|
{
|
||||||
clock_gettime(clockid, &tp);
|
clock_gettime(clockid, &tp);
|
||||||
|
uint64_t cur = ((tp.tv_sec-te.tv_sec)*1000000000 + (tp.tv_nsec-te.tv_nsec));
|
||||||
|
roll(&d, cur);
|
||||||
|
te = tp;
|
||||||
}
|
}
|
||||||
printf("%s: %lu ns\n", name, (tp.tv_sec-te.tv_sec)*10 + (tp.tv_nsec-te.tv_nsec)/100000000);
|
printf("%s: %lu ns, stddev %.2f ns\n", name, (te.tv_sec-ts.tv_sec)*10 + (te.tv_nsec-ts.tv_nsec)/100000000, sqrt(dev(&d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_rdtsc()
|
void test_rdtsc()
|
||||||
|
|
Loading…
Reference in New Issue