Temple of The Roguelike Forums

Development => Programming => Topic started by: Anvilfolk on July 08, 2007, 05:12:35 PM

Title: Matrix or single-array abstraction?
Post by: Anvilfolk on July 08, 2007, 05:12:35 PM
Right, so I've seen that a few people, instead of having a "real" matrix (as in, an array of arrays) use a single array, and when they want to access position (x, y) do something along the lines of array[x + y*width].

Is it really worth it? You're trying to trade two memory accesses for a single one with a little arithmetic on the side, right? Is that speed gain even significant?

Is there something I'm missing?
Title: Re: Matrix or single-array abstraction?
Post by: stu on July 09, 2007, 02:31:02 AM
well its easier to malloc(x*y) than malloc(x) for(j=0;j<x; j++) x[j]=malloc(y)

accessing x[y][z] vs x[y*q + z] makes no difference.

it isnt about speed, its conveniance.

it really makes no difference.

Title: Re: Matrix or single-array abstraction?
Post by: Anvilfolk on July 09, 2007, 11:08:49 AM
Gotcha, thanks!

Having started to use it, it's also a bit more fool-proof than trying to remember to use matrix[y]