-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
103 lines (75 loc) · 2.27 KB
/
main.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
// 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>
#define SIZE 1000000000 // 1 GB
char global_array[SIZE];
char *global_ptr;
size_t dont_optimize_away;
static double get_elapsed_seconds(struct timespec start, struct timespec end) {
return (double)(end.tv_sec - start.tv_sec) + 1.0e-9 * (double)(end.tv_nsec - start.tv_nsec);
}
static void time_static_array(void) {
static char static_arr[SIZE];
struct timespec start;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_t i = 0; i < SIZE; i++) {
static_arr[i] = i;
}
struct timespec end;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
for (size_t i = 0; i < SIZE; i++) {
dont_optimize_away += static_arr[i];
}
printf("time_static_array took %.2f seconds\n", get_elapsed_seconds(start, end));
}
static void time_global_array(void) {
struct timespec start;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_t i = 0; i < SIZE; i++) {
global_array[i] = i;
}
struct timespec end;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
printf("time_global_array took %.2f seconds\n", get_elapsed_seconds(start, end));
}
static void time_local_malloc(void) {
char *local_ptr = malloc(SIZE);
struct timespec start;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_t i = 0; i < SIZE; i++) {
local_ptr[i] = i;
}
struct timespec end;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
free(local_ptr);
printf("time_local_malloc took %.2f seconds\n", get_elapsed_seconds(start, end));
}
static void time_global_malloc(void) {
global_ptr = malloc(SIZE);
struct timespec start;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (size_t i = 0; i < SIZE; i++) {
global_ptr[i] = i;
}
struct timespec end;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
free(global_ptr);
printf("time_global_malloc took %.2f seconds\n", get_elapsed_seconds(start, end));
}
int main(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_t i = 0; i < 2; i++) {
time_static_array();
time_global_array();
time_local_malloc();
time_global_malloc();
printf("\n");
}
}