Author Topic: Matrix or single-array abstraction?  (Read 3864 times)

Anvilfolk

  • High Priest
  • ****
  • Posts: 374
    • View Profile
Matrix or single-array abstraction?
« 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?
"Get it hot! Hit it harder!!!"
 - The tutor warcry

One of They Who Are Too Busy

stu

  • Bishop
  • ***
  • Posts: 138
  • Moop!
    • View Profile
    • Stu's Rusty Bucket
Re: Matrix or single-array abstraction?
« Reply #1 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.

--/\-[ Stu ]-/\--

Anvilfolk

  • High Priest
  • ****
  • Posts: 374
    • View Profile
Re: Matrix or single-array abstraction?
« Reply #2 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]
  • , which is still slightly counter-intuitive to me.
"Get it hot! Hit it harder!!!"
 - The tutor warcry

One of They Who Are Too Busy