Why is it so slow iterating over a big std::list?

后端 未结 6 2170
生来不讨喜
生来不讨喜 2020-12-30 18:08

As title suggests, I had problems with a program of mine where I used a std::list as a stack and also to iterate over all elements of the list. The program was taking way to

6条回答
  •  鱼传尺愫
    2020-12-30 18:29

    There is a cache issue: all data in vector are stored in a contiguous chunk, and each list element is allocated separately and may happen to be stored in quite a random place of memory, which leads to more cache misses. However, I bet that you encounter one of the issues described in the other answers.

提交回复
热议问题