-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc-glib.c
157 lines (132 loc) · 4.21 KB
/
malloc-glib.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
146
147
148
149
150
151
152
153
154
155
156
157
// gcc -O3 malloc-glib.c `pkg-config --cflags --libs glib-2.0` -o malloc-glib
// 30% runtime of this bench is due to malloc
#include <glib.h>
#include <stdio.h>
#include <time.h>
gint compare_ints(gconstpointer a, gconstpointer b) {
return (*(const int*)a - *(const int*)b);
}
void garray_bench() {
GArray *array;
int i;
const int num_entries = 100000000;
// Initialize the GArray
array = g_array_new(FALSE, FALSE, sizeof(int));
// Timing the insertion (append)
clock_t start = clock();
for (i = 0; i < num_entries; i++) {
g_array_append_val(array, i);
}
clock_t end = clock();
printf("GArray: Insertion (append) time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the deletion (removing elements from the end)
start = clock();
for (i = num_entries - 1; i >= 0; i--) {
g_array_remove_index(array, i);
}
end = clock();
printf("GArray: Deletion time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Free the array
g_array_free(array, TRUE);
}
void glist_bench() {
GList *list = NULL;
int i;
const int num_entries = 100000000;
// Timing the insertion (prepend)
clock_t start = clock();
for (i = 0; i < num_entries; i++) {
int *value = g_malloc(sizeof(int));
*value = i;
list = g_list_prepend(list, value); // Inserting at the head
}
clock_t end = clock();
printf("GList: Insertion (prepend) time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the lookup (traverse the list)
start = clock();
GList *l;
for (l = list; l != NULL; l = l->next) {
int *value = (int *)l->data;
}
end = clock();
printf("GList: Lookup (traverse) time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the deletion
start = clock();
while (list != NULL) {
g_free(list->data); // Free the data
list = g_list_delete_link(list, list); // Remove the node
}
end = clock();
printf("GList: Deletion time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
}
void ghashtable_bench() {
GHashTable *hash_table;
int i;
const int num_entries = 100000000;
// Initialize the hash table
hash_table = g_hash_table_new(g_int_hash, g_int_equal);
// Timing the insertion
clock_t start = clock();
for (i = 0; i < num_entries; i++) {
int *key = g_malloc(sizeof(int));
int *value = g_malloc(sizeof(int));
*key = i;
*value = i * 2;
g_hash_table_insert(hash_table, key, value);
}
clock_t end = clock();
printf("GHashTable: Insertion time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the lookup
start = clock();
for (i = 0; i < num_entries; i++) {
int key = i;
g_hash_table_lookup(hash_table, &key);
}
end = clock();
printf("GHashTable: Lookup time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Free the memory
g_hash_table_destroy(hash_table);
}
void gtree_bench() {
GTree *tree;
int i;
const int num_entries = 50000000;
// Initialize the GTree
tree = g_tree_new(compare_ints);
// Timing the insertion
clock_t start = clock();
for (i = 0; i < num_entries; i++) {
int *key = g_malloc(sizeof(int));
int *value = g_malloc(sizeof(int));
*key = i;
*value = i * 2;
g_tree_insert(tree, key, value);
}
clock_t end = clock();
printf("GTree: Insertion time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the lookup
start = clock();
for (i = 0; i < num_entries; i++) {
int key = i;
g_tree_lookup(tree, &key);
}
end = clock();
printf("GTree: Lookup time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Timing the deletion
start = clock();
for (i = 0; i < num_entries; i++) {
int key = i;
g_tree_remove(tree, &key);
}
end = clock();
printf("GTree: Deletion time: %f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
// Destroy the tree
g_tree_destroy(tree);
}
int main() {
garray_bench();
glist_bench();
ghashtable_bench();
//gtree_bench();
return 0;
}