-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmallocBench.c
More file actions
62 lines (51 loc) · 1.38 KB
/
mallocBench.c
File metadata and controls
62 lines (51 loc) · 1.38 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
// mallocBench.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static inline long long ns_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
void bench_small(size_t n) {
void **ptrs = malloc(sizeof(void*) * n);
long long t0 = ns_time();
for (size_t i = 0; i < n; i++)
ptrs[i] = malloc(32);
for (size_t i = 0; i < n; i++)
free(ptrs[i]);
long long t1 = ns_time();
printf("[malloc] Small allocs: %.2f ms\n", (t1 - t0)/1e6);
free(ptrs);
}
void bench_random(size_t n) {
void **ptrs = malloc(sizeof(void*) * n);
long long t0 = ns_time();
for (size_t i = 0; i < n; i++) {
size_t sz = (rand() % 4000) + 16;
ptrs[i] = malloc(sz);
if ((rand() % 3) == 0)
free(ptrs[i]);
}
long long t1 = ns_time();
printf("[malloc] Random allocs: %.2f ms\n", (t1 - t0)/1e6);
free(ptrs);
}
void bench_realloc(size_t n) {
void **ptrs = malloc(sizeof(void*) * n);
long long t0 = ns_time();
for (size_t i = 0; i < n; i++) {
ptrs[i] = malloc(32);
ptrs[i] = realloc(ptrs[i], 1024);
}
long long t1 = ns_time();
printf("[malloc] Realloc: %.2f ms\n", (t1 - t0)/1e6);
free(ptrs);
}
int main() {
srand(123);
bench_small(400000);
bench_random(200000);
bench_realloc(200000);
return 0;
}