-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed_print.c
51 lines (40 loc) · 1.13 KB
/
speed_print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "cpucycles.h"
#include "speed_print.h"
static int cmp_uint64(const void *a, const void *b) {
if(*(uint64_t *)a < *(uint64_t *)b) return -1;
if(*(uint64_t *)a > *(uint64_t *)b) return 1;
return 0;
}
static uint64_t median(uint64_t *l, size_t llen) {
qsort(l,llen,sizeof(uint64_t),cmp_uint64);
if(llen%2) return l[llen/2];
else return (l[llen/2-1]+l[llen/2])/2;
}
static uint64_t average(uint64_t *t, size_t tlen) {
size_t i;
uint64_t acc=0;
for(i=0;i<tlen;i++)
acc += t[i];
return acc/tlen;
}
void print_results(const char *s, uint64_t *t, size_t tlen) {
size_t i;
static uint64_t overhead = -1;
if(tlen < 2) {
fprintf(stderr, "ERROR: Need a least two cycle counts!\n");
return;
}
if(overhead == (uint64_t)-1)
overhead = cpucycles_overhead();
tlen--;
for(i=0;i<tlen;++i)
t[i] = t[i+1] - t[i] - overhead;
printf("%s\n", s);
printf("median: %llu cycles/ticks\n", (unsigned long long)median(t, tlen));
printf("average: %llu cycles/ticks\n", (unsigned long long)average(t, tlen));
printf("\n");
}