Search code examples
c#performanceunity-game-enginepath-finding

Operating on an Array of objects versus operating on a single object containing arrays of parameters?


I am thinking on making a path finding System and each node must contain Positional Data, The movement cost associated with that and so on. Which Approach would be more Performant, as my target Platform is for mobiles.

Which is better on the performance side of things?

This:

public Class Test{
int x;
float y;
string z;
}

.
.
.

Test[] tests = new Test[100];

for(int i=0; i< 100; ++i){
tests[i].x = 0;
tests[i].y = 0.5f;
tests[i].z = "Hello:";
}

Or this:

public Class Test{
int[] x;
float[] y;
string[] z;
}

.
.
.

Test tests = new Test;
test.x = new int[100];
test.y = new float[100];
test.z = new string[100];

for(int i=0; i< 100; ++i){
tests.x[i] = 0;
tests.y[i] = 0.5f;
tests.z[i] = "Hello:";
}

Solution

  • In your example the performance difference is not essential, but the first variant is better in terms of code readability and flexibility.

    You can use struct instead of class to avoid additional dereferencing operations, it will probably increase performance.