-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
52 lines (44 loc) · 1.29 KB
/
main.c
File metadata and controls
52 lines (44 loc) · 1.29 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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
int main() {
// Get the number of rounds.
char *strRounds = getenv("rounds");
if (strRounds == NULL) {
printf("*** The 'rounds' environment variable is not set\n");
abort();
}
char *endptr;
errno = 0;
long long rounds = strtoll(strRounds, &endptr, 10);
if (errno != 0 || *endptr != '\0' || endptr == strRounds) {
printf("*** The 'rounds' environment variable must be an integer, saw: %s\n", strRounds);
abort();
}
double sum = 0.0;
double flip = -1.0;
double pi;
struct timespec tStart, tStop;
long long ix;
// Prime the caches.
for (ix = 1; ix <= 3; ix++) {
flip *= -1.0;
sum += flip / (double)(ix + ix - 1);
}
sum = 0.0;
flip = -1.0;
// Timed test.
clock_gettime(CLOCK_MONOTONIC, &tStart);
for (ix = 1; ix <= rounds; ix++) {
flip *= -1.0;
sum += flip / (double)(ix + ix - 1);
}
pi = sum * 4.0;
clock_gettime(CLOCK_MONOTONIC, &tStop);
// Report.
double tDeltaSecs = (tStop.tv_sec - tStart.tv_sec) +
(tStop.tv_nsec - tStart.tv_nsec) / 1e9;
printf("C,%lld,%.3f,%.40f\n", rounds, tDeltaSecs, pi);
return 0;
}