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

前端 未结 3 1498
走了就别回头了
走了就别回头了 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 00:37

    Impossible to say. As with any performance related test, the answer my vary by any one or more of your OS, your CPU, your memory, your compiler etc.

    So you need to test for yourself. Set your performance targets, measure, optimise, repeat.

    0 讨论(0)
  • 2021-01-15 01:00

    It depends on the common access patterns. If you plan to iterate over the data, accessing every element as you go, the struct approach is better. If you plan to iterate independently over each component, then parallel arrays are better.

    This is not a subtle distinction, either. With main memory typically being around two orders of magnitude slower than L1 cache, using the data structure that is appropriate for the usage pattern can possibly triple performance.

    I must say, though, that your approach to implementing parallel arrays leaves much to be desired. You should simply declare three arrays instead of getting "clever" with two-dimensional arrays and casting:

    int nums[900000000];
    char names[900000000][4];
    float vals[900000000];
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题