Which is most cache friendly?

后端 未结 4 1920
生来不讨喜
生来不讨喜 2021-02-08 03:08

I am trying to get a good grip on data oriented design and how to program best with the cache in mind. There\'s basically two scenarios that I cannot quite decide which is bette

4条回答
  •  故里飘歌
    2021-02-08 03:42

    I recommend profiling with either perf or oprofile and posting your results back here (assuming you are running linux), including the number of elements you iterated across, number of iterations in total, and the hardware you tested on.

    If I had to guess (and this is only a guess), I'd suspect that the first approach might be faster due to the locality of data within each structure, and hopefully the OS/hardware can prefetch additional elements for you. But again, this will depend on cache size, cache line size, and other aspects.

    Defining "better" is interesting too. Are you looking for overall time to process N elements, low variance in each sample, minimal cache misses (which will be influenced by other processes running on your system), etc.

    Don't forget that with STL vectors, you are also at the mercy of the allocator... e.g. it can decide at any time to reallocate the array, which will invalidate your cache. Another factor to try to isolate if you can!

提交回复
热议问题