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

Anvilfolk

  • Rogueliker
  • ***
  • Posts: 374
  • Karma: +0/-0
    • 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

  • Rogueliker
  • ***
  • Posts: 138
  • Karma: +0/-0
  • 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

  • Rogueliker
  • ***
  • Posts: 374
  • Karma: +0/-0
    • 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