Search code examples
c++performancelistdeque

std::deque or std::list


I am using push_front() and push_back() only. Thus, I do not incur any other cost of using insert() or remove().

I know both containers offer O(1) complexity for each of these functions, deques having amortized constant time compared to lists having constant time.

But I want to know which time is less than the other, if there is, at all.


Solution

  • Not sure how to answer your question. You seem to want us to write a benchmark program that you could easily write yourself. So instead of doing that, I'll just state this:

    • With a list, every item you push will incur a memory allocation.
    • With a deque, large blocks are allocated at once.

    Given that memory allocation is normally slow, I would expect the deque to out-perform list.

    If you push or pop many items at once, this will be especially true, as cache locality comes into play.

    Of course, you could write an allocator on the list to use a memory pool. Then you might get better performance.

    So with these hypotheses in mind, go away and measure it, and if you want to discuss the results, that would be the time to ask a question.