当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Optimisation linked list

问题描述:

Silly question maybe - but, what would be faster:

-Deleting an item from a linked list every time an item is gone (whatever that item might be)

-Just marking the record as dead and overwriting it after x amount of time or conditions.

Would I not use less cpu time by avoiding all the removing and inserting rather than just overwriting.

网友答案:

Only way to find out is to use a profiler.

Keep in mind that there are other factors which are very important that you didn't specify in your question. For example, it makes a big difference if you know which element to delete, i.e. you have the pointer to it ready, or if you have to iterate over the linked list to find it. In that case, a vector could actually be several times faster than any linked list (depending on the number of elements) due to caching.

网友答案:

Overwriting is going to be faster. If deleting involves freeing allocated memory from the heap, then overwriting will definitely be faster. Memory allocation is relatively slow. Plus, over time your list memory would get fragmented which might cause cache misses. Even freeing memory that goes back into a memory manager of some sort would be slower since you have the overhead of managing the nodes of the list.

分享给朋友:
您可能感兴趣的文章:
随机阅读: