/
benchmark.c
145 lines (129 loc) · 2.3 KB
/
benchmark.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <time.h>
#include "src/list.h"
static void
bm(char *label, void (*fn)()) {
printf(" %18s", label);
fflush(stdout);
fn();
}
static int nnodes = 10000000;
static clock_t startTime;
static void
start() {
startTime = clock();
}
static void
stop() {
float duration = (float) (clock() - startTime) / CLOCKS_PER_SEC;
printf(": \x1b[32m%.4f\x1b[0ms\n", duration);
}
static void
bm_push() {
start();
int n = nnodes;
List *list = List_new();
while (n--) {
List_push(list, ListNode_new("foo"));
}
stop();
}
static void
bm_unshift() {
start();
int n = nnodes;
List *list = List_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
}
stop();
}
static void
bm_find() {
int n = nnodes;
List *list = List_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
}
List_push(list, ListNode_new("bar"));
start();
List_find(list, "bar");
stop();
}
static void
bm_iterate() {
int n = nnodes;
List *list = List_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
}
ListIterator *it = ListIterator_new(list, LIST_HEAD);
ListNode *node;
start();
while ((node = ListIterator_next(it)))
;
stop();
}
static void
bm_pop() {
int n = nnodes;
List *list = List_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
}
ListNode *node;
start();
while ((node = List_pop(list)))
;
stop();
}
static void
bm_shift() {
int n = nnodes;
List *list = List_new();
while (n--) {
List_unshift(list, ListNode_new("foo"));
}
ListNode *node;
start();
while ((node = List_shift(list)))
;
stop();
}
static List *list;
static void
bm_at() {
start();
List_at(list, 100000);
stop();
}
static void
bm_at2() {
start();
List_at(list, 1000000);
stop();
}
static void
bm_at3() {
start();
List_at(list, -100000);
stop();
}
int
main(int argc, const char **argv){
int n = nnodes;
list = List_new();
while (n--) List_unshift(list, ListNode_new("foo"));
puts("\n 10,000,000 nodes\n");
bm("pushed", bm_push);
bm("unshifted", bm_unshift);
bm("pop", bm_pop);
bm("shift", bm_shift);
bm("find (last node)", bm_find);
bm("iterate", bm_iterate);
bm("at(100,000)", bm_at);
bm("at(1,000,000)", bm_at2);
bm("at(-100,000)", bm_at3);
puts("");
return 0;
}