3 ms·
How did the author ensure the memory allocations were not contiguous? I suppose in C/C++ it's relatively straightforward since you could allocate a bunch, then
by metadat 3y ago
How did the author ensure the memory allocations were not contiguous? I suppose in C/C++ it's relatively straightforward since you could allocate a bunch, then free 99% of it, repeatedly.
Not sure how to do this in Java, Go, or Rust.
- gizmo686 3y agoCode 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)
- tsimionescu 3y agoThe same trick should work in Rust as in C or C++. In Java you could create a linked list class, pin the nodes in memory so that the GC doesn't compact them , then apply the same shuffling. In Go you can go back to the C trick, since Go's garbage collector doesn't do any kind of compaction.