Which kind of data organization using C arrays makes fastest code and why?

前端 未结 3 1515
走了就别回头了
走了就别回头了 2021-01-15 00:15

Given following data, what is the best way to organize an array of elements so that the fastest random access will be possible?

Each element has some int number,

3条回答
  •  长发绾君心
    2021-01-15 01:00

    The first one is probably faster, since memory access latency will be the dominant factor in performance. Ideally you should access memory sequentially and contiguously, to make best use of loaded cache lines and reduce cache misses.

    Of course the access pattern is critical in any such discussion, which is why sometimes it's better to use SoA (structure of arrays) and other times AoS (array of structures), at least when performance is critical.

    Most of the time of course you shouldn't worry about such things (premature optimisation, and all that).

提交回复
热议问题