3 ms·
Code is here: https://github.com/emilk/ram_bench/blob/master/list_traversal.c https://github.com/emilk/ram_bench/blob/master/list_traversa... The author makes
by gizmo686 3y ago
Code is here: https://github.com/emilk/ram_bench/blob/master/list_traversal.c https://github.com/emilk/ram_bench/blob/master/list_traversa...
The author makes 2 allocations:
Node* memory = (Node*)malloc(N * sizeof(Node));
Node** nodes = (Node**)malloc(N * sizeof(Node*));
Of those, only the first one (memory) is actually used during measurement, The seconds one (nodes) is just there to help shuffle the linked list, and is freeded before the actual test:
free(nodes); // Free up unused memory before meassuring:
// Do the actual measurements:
Int start = clock();
...
Since the linked list is shuffeled, the nodes will be linked in a random order. However, since the total memory is contigous, that is the only factor (instead of having every node be a separate allocation, which could allow the system to put every node on a separate page)