-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmath_test.c
154 lines (136 loc) · 3.13 KB
/
math_test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
double rand_double()
{
return (double) rand()/(double) RAND_MAX;
}
int main()
{
clock_t s_time, e_time;
int i, j, nmax=10000;
double a=3.41, b=7.25, c=1.41487;
double *v1, *v2, x, dx;
v1 = malloc(nmax*sizeof *v1);
v2 = malloc(nmax*sizeof *v2);
for (i=0;i<nmax;i++){
v1[i] = rand_double();
v2[i] = rand_double();
}
// default
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += dx;
}
}
e_time = clock();
printf("default sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// 2x
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += dx*dx;
}
}
e_time = clock();
printf("multi sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// pow(2)
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += pow(dx,2);
}
}
e_time = clock();
printf("pow 2 sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// pow(2.1)
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += pow(dx,2.1);
}
}
e_time = clock();
printf("pow 2.1 sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// exp
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += exp(-dx);
}
}
e_time = clock();
printf("exp sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// sin
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += sin(-dx);
}
}
e_time = clock();
printf("sin sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// regular equation
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += a*pow(dx,4) + b*pow(dx,2) + c*dx;
}
}
e_time = clock();
printf("regular eqn sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// expanded equation
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += a*dx*dx*dx*dx + b*dx*dx + c*dx;
}
}
e_time = clock();
printf("expanded eqn sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// optimized equation
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += ((a*dx*dx + b)*dx + c)*dx;
}
}
e_time = clock();
printf("optimized eqn sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
// 2nd optimized equation
s_time = clock();
x = 0.0;
for (i=0;i<nmax;i++){
for (j=0;j<nmax;j++) {
dx = abs(v1[i] - v2[j]);
x += c*dx;
dx = dx*dx;
x += dx*(b + dx*a);
}
}
e_time = clock();
printf("2nd optimized eqn sum %f %f \n", (e_time - s_time)/(double) CLOCKS_PER_SEC, x);
free(v1);
free(v2);
return 0;
}