On Mar 27, 7:49 pm, Pavel
Functionally, the way suggested by Alf should work fine, but if you want
to squeeze the maximum performance, consider keeping a memory as a
1-dimensional vector (of 10000, in your case). This way, you will avoid
an extra indirection when accessing a field. (vector of vectors is
actually more flexible than the rectangular game field you want: you can
have every of lower-level vectors of different size; but this unwanted
flexibility comes at little space and sometimes significant time costs).
Ok, it's not obvious to me that myData[x][y] will be slower
than yourData[x*width+y]. It's also not obvious it won't be.
Especially if you have to do some passing of values into
functions that de-ref the array. And depending on how much
data is actually contained in the arrray, and if the platform
has to do some work (swapping etc.) to get data that is very
far away.
Before choosing between these two, some considerations:
- Is there in fact a difference at all? And is it significant?
- How important is the relative difference to the performance
of the specific application?
This would require some actual benchmark tests on the
platform involved, using test code that was as realistic
as reasonably achievable. And it would require some
profiling work on the code to find out if it spends a
lot of time in this specific aspect. If it does just
*tonnes* of references into the array then it *might*
be worthwhile. On the other hand, if it does one
reference into the array each time the player makes
a move, then you might not see any noticible improvement
from the player's point of view.
Which brings up another point: It may be that the code
is easier for the current crop of coders to read if one
or the other form is used. So either way might easily
be preferred from the point of view of maintenance.
Maintenance may, or may not, be hugely important to a
game code. Frequently game code is pitched out and you
start over for the next version, because frequently you
are trying to cozy up to the fastest game machines.
So, with respect to this suggestion, it depends.
Socks