Saturday 16 February 2019

c - Arrays or linked-list for frequently random access?



I'm using lists and arrays very often, I am wondering what is faster, array or list?



Let's say we have array of integers and linked-list, both hold same values.




int array_data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

typedef struct node_data{
int data;
struct node_data * next;
} list_data;

____ ____ ____ ____ ____ ____ ____ ____ ____ ____
list_data *d = -> | 1| -> | 2| -> | 3| -> | 4| -> | 5| -> | 6| -> | 7| -> | 8| -> | 9| -> |10| -> NULL



If I want to get value of array_data on index 6, I'll use array_data[6] and get value of 7. But what if I want same data from list? Should I go from start and count hops till I reach asked index get_data(d,6)? Or there is better way to do this?



int get_data(list_data *l, int index){
int i = 0;
while(l != NULL){
if(index == i){
return l -> data;
}
i++;

l = l -> next;
}
return 0;
}


How about using array of pointers to list elements? Will be this best solution in case I have more then 100,000 records or even more to save, and each record contains more then one data type. I'll mostly need insertion to the end, and very frequently access to elements.



Thanks!


Answer




You are correct to consider the question each time; when deciding whether to implement an array, linked-list (or other) structure.



ARRAY




  • + Fast random access.

  • + Dynamically allocated arrays can be re-sized using `realloc()`.

  • + Sorted using `qsort()`.

  • + For sorted arrays, a specific record can be located using `bsearch()`


  • - Must occupy a contiguous block of memory.


  • - For a long-lived applications, frequent enlargement of the the array can eventually lead to a fragmented memory space, and perhaps even eventual failure of `realloc()`.

  • - Inserting and deleting elements is expensive. Inserting an element (in a sorted array) requires all elements of the array beyond the insertion point to be moved. A similar movement of elements is required when deleting an element.


LINKED-LIST


  • + Does not require a contiguous block of memory.

  • + Much more efficient than an array to re-size dynamically. Outperforms an array when it comes to fragmented memory usage

  • + Sequential access is good, but perhaps still not as fast as an array (due to CPU cache misses, etc.).


  • - Random access is not really possible.


  • - Extra memory overhead for node pointers (priorNode, nextNode).



There are other structures that even combine arrays with linked list, such as hash tables, binary trees, n-trees, random-access-list, etc., each comes with various characteristics to consider.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...