Jul 26, 2021

Use temporary arrays for Look-Up Tables instead of switches

tl;dr You should use temporary arrays instead of switches in Game Maker where applicable if you want to squeeze out a few more CPU cycles without cluttering RAM, but if switches are your thing, don't feel ashamed continuing to use them.

 

I have been using buffers for large swaths of data inside LUTs, as this is generally easier to type out in my opinion, take up much less RAM, and are permissively slower than arrays. However, smaller sets of data I have taken to just using switches or a series of switches. I pondered if it might possibly be faster to just type out the LUT in a temporary array. In Game Maker Studio 2, this could be done in one line, although I'm using GMS1. Part of me was hoping it would be much faster, but as I started to type out a LUT array for my test, I considered the act of populating the array would slow things down considerably. Even making sure I opened the array with the highest index (this is a must in Game Maker, so if you aren't doing that already, make it a habit), the test results showed that a temporary array was marginally faster than using a switch, but it was a noticeable difference across all tests.

I basically ran four tests: 

  1. a small LUT (8 entries), 
  2. a larger LUT (16 entries), 
  3. a duo LUT (2x 8 entries) across two switches, 
  4. and a duo LUT within one switch. 

An example of when tests 3 or 4 would arise in my code is having a LUT of coordinate pairs, where each LUT is 8 entries long, but x could be defined by 5 cases and y could be defined by 4 cases. Each coordinate would need its own array; using switches, I could give each coordinate its own smaller switch, or define both coordinates within a single full switch. The results for tests 1 and 2 were practically identical, with a switch consistently being slower than a properly defined array regardless of size. The results of test 3 showed using more than one switch was a horrible idea, significantly increasing the processor time. I actually got the results I was looking for from test 4, which is that one switch to handle multiple LUTs is arguably a smidge faster than arrays, but here the difference is unarguably negligible. 

 I don't know if I'll go back and rewrite my switches as temporary arrays, but I do find the array method to be easier on the eyes.

©TheouAegis Productions™. Powered by Blogger.