c - malloc cpu cycles -
what cost of malloc(), in terms of cpu cycles? (vista/os, latest version of gcc, highest optimization level,...)
basically, i'm implementing complex dag structure (similar linked list) composed of 16b (less common) , 20b nodes (more common).
occasionally, have remove nodes , add some. but, rather using malloc() , free(), can move unneeded nodes end of data structure, , update fields algorithm continues. if free node available, update fields; if not, i'll have allocate new one.
the problem is, might have 1 free node available while having input, example, 20 nodes worth of data. means:
- i check available free node
- the check succeed, , free node updated
- i check available node 19 more times
- all checks fail, , malloc() called each time
question: worth it? should malloc() , free() usual, or worth keep free nodes available @ end of list, , keep checking if fail , lead malloc() anyway?
more concretely,
what cpu cost of malloc()??
does matter costs? really?
the true answer "it depends".
it depends on loads of things
- what else os doing @ time
- how fragmented memory has become
- speed of memory , processor on client pc
- etc
if code massively performance critical, them time everything can , work out best pattern usage case.
if isn't performance critical bit of code, whatever clearest , simplest implement , maintain.
"we should forget small efficiencies, 97% of time: premature optimization root of evil", donald knuth
Comments
Post a Comment