Hello all. Toward the end of today’s Corona Geek Hangout (#177), I talked a little bit about the idea of ‘object caching’.
In a nutshell, object caching is the practice of keeping old display objects and re-using them instead of creating new display objects and destroying them when you are done.
No Caching
- Create an object.
- Interact with it a bit.
- Destroy it via obj:removeSelf() or dipsplay.remove( obj )
Object Caching
- Create an object.
- Interact with it a bit.
- Stop using it and move it somewhere off screen and/or hide it
- …
- Then, later when you need another object of the same sort, you simply move the object back on screen where you need it instead of creating a new one.
The question that exists is, “Is all this worth it? i.e. Does the performance gain make the extra coding worthwhile?”
My answer to this question for the longest time was, “probably not.” I never use this technique, or at least not till recently when we started making the “Geometry Wars - Retro Evolved Clone”.
To further explore this idea, I made a basic testch bench: http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/Tips_and_Tricks/objectCaching.zip
I am writing this post to ask people to run the test bench and to then post back their results below.
I will post my findings (and update them over time) as the next entry in this post.
Running the Test Bench
To run the test bench do the following:
- Build the app and install it on your device(s) of choice:
- Start the app.
- Wait of the frame rate counter to initialize
- Click 1 - For circle/rect/imagerect/sprite
- Click 400
- Click ‘Use Caching’
- Wait…If the frame rate settles on a value below 60, write it down. Otherwise KILL the app and choose a higher value during step 5.
- Once you have recorded a value, KILL the app and repeat the test using ‘No Caching’. Wait and write down the number for that setting.
Do this test for 1 (circles), 2 (newRect), 3 (newImageRect), and 4 (newSprite).
At the end you should have 8 values written down. Be sure to track your settings too.
Finally, post back your finding below.
If this is confusing, please see the GIF below and look at the values I’ve posted below for my devices.
Thanks!
PS- This may not produce useful values in the simulator so don’t bother with that…
PPS - Forgot to say what the benchmark is doing:
For 500 object setting, it creates/restores 500 objects in frame 1, removes them in frame 2, creates/restores them anew in frame 3, removes them all in frame 4, and so on…
So, at an ideal rate of 60 FPS, this would equate to creating and destroying 15,000 objects per second… ideally.