- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
Latest commit
103 lines (75 loc) · 2.27 KB
/
Copy pathmain.c
File metadata and controls
103 lines (75 loc) · 2.27 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
// Solely so stupid VS Code can find "CLOCK_PROCESS_CPUTIME_ID"
#define_POSIX_C_SOURCE 199309L
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#defineSIZE 1000000000 // 1 GB
charglobal_array[SIZE];
char*global_ptr;
size_tdont_optimize_away;
staticdoubleget_elapsed_seconds(structtimespecstart, structtimespecend) {
return (double)(end.tv_sec-start.tv_sec) +1.0e-9* (double)(end.tv_nsec-start.tv_nsec);
}
staticvoidtime_static_array(void) {
staticcharstatic_arr[SIZE];
structtimespecstart;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_ti=0; i<SIZE; i++) {
static_arr[i] =i;
}
structtimespecend;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
for (size_ti=0; i<SIZE; i++) {
dont_optimize_away+=static_arr[i];
}
printf("time_static_array took %.2f seconds\n", get_elapsed_seconds(start, end));
}
staticvoidtime_global_array(void) {
structtimespecstart;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_ti=0; i<SIZE; i++) {
global_array[i] =i;
}
structtimespecend;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
printf("time_global_array took %.2f seconds\n", get_elapsed_seconds(start, end));
}
staticvoidtime_local_malloc(void) {
char*local_ptr=malloc(SIZE);
structtimespecstart;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_ti=0; i<SIZE; i++) {
local_ptr[i] =i;
}
structtimespecend;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
free(local_ptr);
printf("time_local_malloc took %.2f seconds\n", get_elapsed_seconds(start, end));
}
staticvoidtime_global_malloc(void) {
global_ptr=malloc(SIZE);
structtimespecstart;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_ti=0; i<SIZE; i++) {
global_ptr[i] =i;
}
structtimespecend;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
free(global_ptr);
printf("time_global_malloc took %.2f seconds\n", get_elapsed_seconds(start, end));
}
intmain(void) {
printf("Before pages have been mapped:\n");
time_static_array();
time_global_array();
time_local_malloc();
time_global_malloc();
printf("\nAfter pages have been mapped:\n");
for (size_ti=0; i<2; i++) {
time_static_array();
time_global_array();
time_local_malloc();
time_global_malloc();
printf("\n");
}
}