Game Development Reference
In-Depth Information
Row Order or Column Order?
Not every language defines arrays in row order. Some versions of PASCAL
define arrays in column order. Don
'
t make assumptions unless you like
writing slow code.
If you access an array in the wrong order, it will create a worst-case CPU cache
scenario. Here
'
s an example of two functions that access the same array and do the
same task. One will run much faster than the other:
const int g_n = 500;
float TestData[g_n][g_n][g_n];
inline void column_ordered()
{
for (int k=0; k<g_n; k++)
// K
for (int j=0; j<g_n; j++)
// J
for (int i=0; i<g_n; i++)
// I
TestData[i][j][k] = 0.0f;
}
inline void row_ordered()
{
for (int i=0; i<g_n; i++)
// I
for (int j=0; j<g_n; j++)
// J
for (int k=0; k<g_n; k++)
// K
TestData[i][j][k] = 0.0f;
}
The timed output of running both functions on my test machine showed that acces-
sing the array in row order was over 10 times faster:
Column Ordered: 3531 ms
Row Ordered: 297 ms
Delta: 3234 ms
Any code that accesses any largish data structure can benefit from this technique. If
you have a multistep process that affects a large data set, try to arrange your code to
perform as much work as possible in smaller memory blocks. You
ll optimize the use
of the L2 cache and make a much faster piece of code. While you surely won
'
t have
any piece of runtime game code do something this crazy, you might very well have a
game editor or production tool that does.
'
 
Search WWH ::




Custom Search