I was asked by someone to determine which one of codes would run faster. No doubt, I went with the first one as it looked much obvious to me but I didn't exactly know why the first one would be faster. P.S. I tried both of the codes and based of my observation the first approach is faster.
First approach:
struct A
{
int x[32];
};
A* arr = new A[100];
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 32; j++)
{
arr[i].x[j] = 1;
}
}
The second approach:
struct A
{
int x[32];
};
A* arr = new A[100];
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 100; j++)
{
arr[j].x[i] = 1;
}
}