-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcompute_time.c
More file actions
155 lines (140 loc) · 2.84 KB
/
compute_time.c
File metadata and controls
155 lines (140 loc) · 2.84 KB
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
155
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main()
{
clock_t t1, t2; // variables for computing clocks
double a=1.234, b=2.456, c;
double T1, T2;
int i, j, k, N=100000000;
t1 = clock();
for(i=0;i<N;++i)
{
c = b;
b = a;
a = c;
}
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("mem x 3 + 1 loop:%f\n",T1);
printf("(a, b, c)=%f %f %f\n", a, b, c);
t1 = clock();
for(i=0;i<N;++i)
{
c = b;
b = a;
a = c;
c = b;
b = a;
a = c;
}
t2 = clock();
T2 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("mem x 6 + 1 loop:%f\n",T2);
printf("mem:%f\n",(T2-T1)/3.0);
printf("(a,b,c)=%f %f %f\n", a, b,c);
printf("====================================\n");
//以下為 a 與 b 互換的程式 (計算加法所需的時間)
t1 = clock();
for(i=0;i<N;++i)
{
a = a+b;
b = a-b;
a = a-b;
}
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(+,-) time x 3 + 1 loop:%f\n",T1);
printf("(a,b)=%f %f\n", a,b);
t1 = clock();
#pragma omp parallel //平行計算
for(i=0;i<N;++i)
{
a = a+b;
b = a-b;
a = a-b;
a = a+b;
b = a-b;
a = a-b;
}
t2 = clock();
T2 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(+,-) time x 6 + 1 loop:%f\n",T2);
printf("Real (+,-) time: %f\n",(T2-T1)/3.0);
printf("(a,b)=%f %f\n", a,b);
printf("====================================\n");
//以下為 a 與 b 互換的程式 (計算乘法所需的時間)
t1 = clock();
for(i=0;i<N;++i)
{
a = a*b;
b = a/b;
a = a/b;
}
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(*,/) time x 3 + 1 loop:%f\n",T1);
printf("(a,b)=%f %f\n", a,b);
t1 = clock();
for(i=0;i<N;++i)
{
a = a*b;
b = a/b;
a = a/b;
a = a*b;
b = a/b;
a = a/b;
}
t2 = clock();
T2 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(*,/) time x 6 + 1 loop:%f\n",T2);
printf("(*,/) time:%f\n",(T2-T1)/3.0);
printf("(a,b)=%f %f\n", a,b);
printf("====================================\n");
//以下為計算 sin 時間的程式
t1 = clock();
for(i=0;i<N;++i)
{
a = sin(a);
}
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(sin) time + 1 loop:%f\n",T1);
printf("(a,b)=%f %f\n", a,b);
t1 = clock();
for(i=0;i<N;++i)
{
a = sin(b);
b = sin(a);
}
t2 = clock();
T2 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(sin) time x 2 + 1 loop:%f\n",T2);
printf("(sin) time: %f\n",T2-T1);
printf("(a,b)=%.16e %.16e\n", a,b);
printf("====================================\n");
//以下為計算 "隨機取亂數" 所需時間的程式
srand(time(NULL));
short r;
//有平行...花的時間反而比較多?!
#pragma omp parallel for
t1 = clock();
for(i=0;i<N;i++)
{
r = rand();
}
t2 = clock();
printf("(rand) parallel time:%f\n",(t2-t1)/(double) CLOCKS_PER_SEC);
printf("r=%d\n", r);
//未平行
t1 = clock();
for(i=0;i<N;i++)
{
r = rand();
}
t2 = clock();
printf("(rand) time:%f\n",(t2-t1)/(double) CLOCKS_PER_SEC);
printf("r=%d\n", r);
return;
}