Skip to main content
gamedev.net
🔒 Locked

Performance: Single array vs. multiple arrays

Started by Russell Mar 9, 2003 at 10:18 PM 5 replies 300+ views
Original Post
Russell
Russell
From a performance standpoint, which of the following would be preffered? 1. Single array of structs/classes
typedef struct {
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
} MyStruct;

MyStruct array[256];
 
2. Multiple arrays
unsigned char a[256];
unsigned char b[256];
unsigned char c[256];
unsigned char d[256];
 
It seems like a single array would be preferred from both a performance standpoint, and from a good coding practice standpoint as well (easier to read/understand/organize/etc/).
SiCrane
SiCrane
It depends on how you use them. If you do work on all the a''s first and then all the b''s, etc. multiple arrays will be faster. If when you process your data, a[0], b[0], c[0], d[0] all get used at the same time, (and then a[1], b[1], c[1], d[1]) then the single array will be faster.
Codican
Codican
Use whichever one makes your code easier to read. Leave that kind of performance optimisation to the compiler.

-C
Waverider
Waverider
One primary reason for using separate arrays would be the speed of the program. For instance, if you have an array of game objects and are performing collision detection, yet many of the objects are just particles (so you would just skip over them), it would speed up the collision detection to store the particles in a separate array.
It's not what you're taught, it's what you learn.
SiCrane
SiCrane
quote:
Original post by Codican
Use whichever one makes your code easier to read. Leave that kind of performance optimisation to the compiler.

-C


The C and C++ standards place explicit limitations on the layout of structures in memory, so no compliant compiler will automatically transform one form to the other, and instruction reordering can only do so much if there are true data dependencies. Simply put, no compiler is capable of doing this kind of performance optimization.
Shag
Shag

      
struct MY_STRUCT
{
unsigned char *a;
unsigned char *b;
unsigned char *c;
unsigned char *d;
} MyStruct;

MyStruct.a = ( unsigned char * ) _aligned_malloc( sizeof( unsigned char ) ) * 256, 64 );
MyStruct.b = ( unsigned char * ) _aligned_malloc( sizeof( unsigned char ) ) * 256, 64 );
MyStruct.c = ( unsigned char * ) _aligned_malloc( sizeof( unsigned char ) ) * 256, 64 );
MyStruct.d = ( unsigned char * ) _aligned_malloc( sizeof( unsigned char ) ) * 256, 64 );



This will probably be fastest way you can arrange such data.

[edited by - Shag on March 10, 2003 1:42:50 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.