开发者

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 开发者_Python百科complexity point of view?


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.


Without more details on how you are going to use the information, it is a bit tough to comment on the complexity. However, here are a few thoughts:

  • If you use realloc, it would likely be better to realloc to add "some" more items (rather than one each and every time). Typically, a good algorithm is to double the size each time.
  • If you use a linked list, you could speed up the access in a simple post-processing step. Allocate an array of pointers to the items and traverse the list once setting the array elements to each item in the list.
  • If the items are of a fixed size in the file, you could pre-compute the size simply by seeking to the end of the file, determining the size, divide by the item size and you have the result. Even if it is not a fixed size, you could possibly use this as an estimate to get "close" to the necessary size and reduce the number of reallocs required.


as other users already have stated:

Premature optimization is the root of all evil

Donald Knuth

I have a different proposal using realloc: in the C++ STL the std::vector container grows every time an object is inserted and not enough space is available. The size of the growing depends on the current pre-allocated size but is implementation specific. For example, you could save the actual number of preallocated objects. If the size runs out, you call reallocate with the double amount of space as currently allocated. I hope this was somewhat understandable!

The caveeat is of course, that you propably will allocate more space than you actually will consume and need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜