What is the cost of malloc(), in terms of CPU cycles? (Vista/OS, latest version of gcc, highest optimization level,...)
Basically, I'm implementing a complex DAG structure (similar to a linked list) composed of some 16B (less common) and 20B nodes (more common).
Occasionally, I will have to remove some nodes and then add some. But, rather than always using malloc() and free(), I can simply move unneeded nodes to the end of my data structure, and then update the fields as my algorithm continues. If a free node is available, I will update the fields; if not, I'll have to allocate a new one.
The problem is, I might have only one free node available while having to input, for example, 20 nodes worth of data. This means:
- I will check for an available free node
- The check will succeed, and that free node will get updated
- I will check for an available node 19 more times
- All checks will fail, and malloc() will be called each time
Question: Is it really worth it? Should I just malloc() and free() as usual, or is it worth it to keep some free nodes available at the end of the list, and keep checking even if it will usually fail and lead to malloc() anyway?
More concretely,
What is the CPU cost of malloc()??