-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestspeed.c
69 lines (57 loc) · 1.36 KB
/
testspeed.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#define M_PI_2_INV (1.0/M_PI_2)
#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
#define ERF_COEF (1.0/M_2_SQRTPI)
const int SIZE=100;
const int CYCLES=10000000;
FILE *fp0;
double benchmark(const char* name, double (*fun)(double)) {
fp0 = fopen("thing","r+");
clock_t start, stop;
double xs[SIZE];
double t_ns;
for (int i=0; i<SIZE; i++) {
xs[i] = rand();
}
start = clock();
for (int repeat=0; repeat<CYCLES; repeat++) {
for (int i=0; i<SIZE; i++) {
(*fun)(xs[i]);
}
}
stop = clock();
t_ns = (stop-start)*1.0e9/CLOCKS_PER_SEC/CYCLES/SIZE;
printf("%-17s %6.1f ns\n", name, t_ns);
return t_ns;
}
double with_atan(double x) {
/* normalized atan */
return rand();
}
double with_exp(double x) {
//fscanf(fp0,"%f", &x);
fseek(fp0,0,SEEK_SET);
return x;
}
double with_sqrt(double x) {
fseek(fp0,0,SEEK_SET);
return x;
}
double with_erf(double x) {
return erf(ERF_COEF*x);
}
double with_fabs(double x) {
return x/(1.0 + fabs(x));
}
int main(int argc, char **argv) {
//benchmark("scanning", with_atan);
benchmark("1/(1+exp(-x))", with_exp);
benchmark("1/sqrt(1+x^2)", with_sqrt);
benchmark("erf(sqrt(pi)*x/2)", with_erf);
benchmark("tanh(x)", tanh);
benchmark("x/(1+|x|)", with_fabs);
}