Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realloc Vs Linked List Scanning

I have to read from a file an unknown number of rows and save them in to a structure (I would like to avoid a prepocessing to count the total number of elements). After the reading phase I have to make some computations on each of the elements of these rows.

I figured out two ways:

  1. Use realloc each time I read a row. This way the allocation phase is slow but the computation phase is easier thanks to the index access.

  2. Use a linked list each time I read a row. This way the allocation phase is faster but the computation phase is slower.

What is better from a complexity point of view?

like image 802
Beppe Avatar asked May 11 '11 14:05

Beppe


1 Answers

How often will you traverse the linked list? If it's only once go for the linked-list. Another few things: vill there be a lot of small allocations? You could make a few smaller buffers for let's say 10 lines and link those togeteher. But that's all a question of profiling.

I'd do the simplest thing first and see if that fits my needs only then i'd think about optimizing.

Sometimes one wastes too much time thinking about the optimum even when the second best solution also fits the needs perfectly.

like image 150
RedX Avatar answered Sep 20 '22 14:09

RedX