Skip to main content

Corona SDK newRect() vs newImageRect() performance comparison

My next Corona game contains quite a lot of explosion effects in pixel art style, where I basically just scatter a number of single-colored squares in different directions. The squares are all created using display.newRect() which I figured had to be both the easiest and most efficent way for creating these simple geometrical shapes.

Explosion close-up from Ice Trap
But then it kind of hit me: What if using newImageRect() with a 1px image scaled to the desired size could actually be faster than newRect()? Not very likely, but since I have no idea how the Corona SDK graphics engine is implemented I had to try it out with a little sample program.

I decided to run three separate tests. Not scientific in any way, but at least it should give me a clue of which function performs best. In each of the tests I just created a bunch of squares of the same size randomized across the screen, and measured the time taken to create them. The three tests had the following setup:

1. Use display.newRect() to create the squares of desired size
2. Use display.newImageRect() and a 1px image, scaling to 10px
3. Use display.newImageRect() and a 10px image, no scaling needed

The results showed that display.newRect() was approximately 15 times faster than using display.newImageRect()! Sure, I had expected newRect() to be faster, but not by a factor this big when it only comes to turning a 1px image into a display object. The tests also showed that loading a 1px image and scaling it to 10px performed just as well as loading a 10px image directly (tests 2 vs 3).

This is a sample log from one of the test run in the Corona simulator, with 10,000 squares created in each test case:

Copyright (C) 2009-2016  C o r o n a   L a b s   I n c .
Version: 3.0.0
Build: 2016.2933
Platform: iPhone / x64 / 10.0 / NVS 4200M/PCIe/SSE2 / 4.5.0 NVIDIA 369.09 / 2016.2933 / sv_SE | SE | sv_SE | sv
Loading project from:   C:\dev\corona-sdk-libs\image-vs-rect
Project sandbox folder: C:\Users\Markus\AppData\Local\Corona Labs\Corona Simulator\Sandbox\image-vs-rect-12200601DCB5402B4F8C4926C65B0502\Documents
Creating 10000 squares using newRect() = 61.6 ms.
Creating 10000 squares using newImageRect() and 1px image = 991.9 ms.
Creating 10000 squares using newImageRect() and 10px image = 1042.8 ms.

And here's the full Lua source code. If you want to try it out, make sure that you have two image files at hand. The code expects them to be named green-1px.png and green-10px.png and located in the same folder as the main.lua file.

Comments