To see more JOM Software applications, click here.
Version 1.00, release date 2008-11-13.
Games quite often require list of lists i.e. matrixes to save and manipulate data. There are several ways to implement such and, as a very experienced expert, I always knew exactly which way was the most efficient way.
Oh, I so hate to be wrong! Especially since I was so certain that I was right, just based on my very experienced gut feeling. Who can you trust, if not your guts?
During latest flu season I wasn't able to do much but think. So I thought about one certain game, which I have never played much, which doesn't interest me too much, but which has been huge success all over the world. There was sample code which kept crashing and which I couldn't "read" too well. It got me interested in this test sample.
This night I was recovered enough to write a small test to compare three different ways to access a single random tile inside a matrix. The result was big surprise to me, since the method I had preferred seems to be the slowest!
T1 is matrix based on dictionary. This is the reason, which got me interested in performance testing. I had never seen anything like that before.
a[(x,y)] = 1
T2 is the way I used to prefer, one simple list where you access a tile with calculated offset. It does have performance advantages, when you handle tiles in rows - or so I still believe :)
b[x + y*dim_x] = 1
T3 is "normal" matrix, a list of lists. I always thought it would be slow or at least not the fastest.
c[x][y] = 1
Disclaimer1: test code was not optimized at all, it's just accessing all tiles in same way. In real life, for example Unity and Mazing Days,
I have optimized cases where tiles are accessed one row at a time.
Random access is very common use case, but in certain type of games
sequential access can be more useful.
Disclaimer2: in real life matrixes are often small and performance differences between these methods are minimal. It's better to use something which you are familiar with than to experiment blindly.
(C) 2008 Jouni Miettunen