include sys times include time include sys time double pi const int st

 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
#include <sys/times.h>
#include <time.h>
#include <sys/time.h>
double pi(const int steps_number) {
int i;
double pi = 4;
for (i = 0; i < steps_number; i++) {
pi += (i % 2 ? 1 : -1) * (4 / (3 + 2*(double)i));
}
return pi;
}
struct timeval tv1, tv2, dtv;
struct timezone tz;
void time_start() {
gettimeofday(&tv1, &tz);
}
long time_stop() {
gettimeofday(&tv2, &tz);
dtv.tv_sec = tv2.tv_sec - tv1.tv_sec;
dtv.tv_usec = tv2.tv_usec - tv1.tv_usec;
if (dtv.tv_usec < 0) {
dtv.tv_sec--;
dtv.tv_usec+=1000000;
}
return dtv.tv_sec*1000+dtv.tv_usec/1000;
}
int main(void) {
long steps_number = 999999999;
double pi_value;
system("sync");
time_start();
pi_value = pi(steps_number);
printf("Time: %ld\n", time_stop());
printf("%.50f\n", pi_value);
return 0;
}