array of structures, or structure of arrays?

后端 未结 11 454
甜味超标
甜味超标 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 01:44

    Notice that the second approach might have negative impact on caching behaviour. If you want to access a single record at a time, you'd better have that record not scattered all across the place.

    Also, the only memory you win in the second approach, is (possibly) due to member alignment. (and having to allocate a separate object). Otherwise, they have exactly the same memory use, asymptotically. The first option is much better due to locality, IMO

提交回复
热议问题