So, the name is somewhat misleading ... I will keep it simple: I am comparing these two data structures:
- An array in which it starts at size 1, and for each subsequent addition there is a call to realloc () to expand the memory, and then add a new (malloced) element at position n-1.
- A linked list in which I track head, tail and size. And the addition includes mallocing for the new element and updating the pointer and tail size.
Do not worry about any other details of these data structures. This is the only function I am involved with this testing.
In theory, LL should work better. However, they are almost identical in time, including 10, 100, 1000 ... up to 5,000,000 elements.
The gut feeling is a big pile. I think the default data segment is 10 megabytes on Redhat? I could be wrong. In any case, realloc () first checks to see if space is available at the end of an already allocated contiguous memory location (0- [n-1]). If the nth position is available, the movement of elements does not occur. Instead, realloc () simply reserves the old space + immediately the next space. It's hard for me to find evidence of this, and I have to complicate the time by proving that this array should work worse in practice than LL.
Following is the following analysis after reading the posts below:
[Update # 1] I changed the code to have a separate list, which mallocs stores every 50th iteration for LL and Array. For 1 million additions to the array of almost 18 moves. There is no concept of transition to LL. I made a time comparison, they are still almost identical. Here is some conclusion for 10 million additions:
(Array) time ./a.out a 10,000,000 real 0m31.266s user 0m4.482s sys 0m1.493s (LL) time ./a.out l 10,000,000 real 0m31.057s user 0m4.696s sys 0m1.297s
I would expect the time to be dramatically different with 18 moves. Adding an array requires one more assignment and one more comparison to get and check the return value of realloc to ensure that the transition has occurred.
[Update # 2] I tested ltrace on the testing I wrote above, and I think this is an interesting result ... It seems that realloc (or some memory manager) proactively moves the array to larger adjacent locations based on the current size. For 500 iterations, iterations triggered a memory run: 1, 2, 4, 7, 11, 18, 28, 43, 66, 101, 154, 235, 358 This is pretty close to the summation sequence. I find it quite interesting - I thought that I would publish it.