What is faster? [x][y] or [y*w+x]?

Started by
33 comments, last by DigitalDelusion 19 years, 6 months ago
y*w+x is certanly _not_slower_ on any reasonable system, that's everything u need to know.

In most cases, if you don't have true multidimensional arrays, y*w+x is faster. In other cases it's might be equally fast but not slower.

If you have true multidimensional arrays, y*w+x should be equally fast .Or something sucks....either your compiler sucks at optimizations really badly, or multidimensional arrays sucks. or both....

Advertisement
Quote:Original post by DEVLiN
*grr* didn't have auto-login at work :)

edit to the above post:
It all boils down to how you will access the data in your application, ofcourse. If you will just iterate through your data, your tests are valid. (if still a bit skewed due to the non-linearity of the linear test).

If you're going to use something similar to a PutPixel(x,y) approach, you really should base the tests on random access.

Just my opinion ofc, but I do believe it's a valid point to make.


Hmm, true.
Depending on usage pattern either approach can be faster, random access is a good example it's actually quite possible that in that scenario keeping the row pointers in a seprate array makes things go faster.

It really boils down to if a mul is faster than fetching the first index from the array, depending on cache utilization that's really an open question so both are "fastest" but in diffrent scenarios.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by DigitalDelusion
Depending on usage pattern either approach can be faster, random access is a good example it's actually quite possible that in that scenario keeping the row pointers in a seprate array makes things go faster.

It really boils down to if a mul is faster than fetching the first index from the array, depending on cache utilization that's really an open question so both are "fastest" but in diffrent scenarios.

i'm pretty sure mul is faster than memory read. That is, as we see, lookup tables for square root is probably useless, and square root is alot slower than multiply.

Especially when you want to do two memory reads/writes consequently(that is, with random access, read the row pointer, read the data from the row.). Also i'm sure it's faster to allocate single piece of memory than many small pieces.
Well obviously for a square array anyone allocating each row independently is doing something insanly stupid.

But cacheing row pointers can sometimes yield a speedup but as I said it depends on your usage pattern doing random x,y access it's actually not very hard to get the [][] approach be slightly faster on big datasets.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement