array of structures, or structure of arrays?

后端 未结 11 520
甜味超标
甜味超标 2021-01-01 01:32

Hmmm. I have a table which is an array of structures I need to store in Java. The naive don\'t-worry-about-memory approach says do this:

public class Record          


        
11条回答
  •  无人及你
    2021-01-01 02:04

    I was curious so I actually ran a benchmark. If you don't re-create the object like you are[1], then SoA beats AoS by 5-100% depending on workload[2]. See my code here:

    https://gist.github.com/twolfe18/8168262c5420c7a62d39

    [1] I didn't add that because if you are concerned enough about speed to consider this refactor, it would be silly to do that.

    [2] This also doesn't account for re-allocation, but again, this is often something you can either amortize away or know statically. This is a reasonable assumption for a pure-speed benchmark.

提交回复
热议问题