Performance of .width .height vs .xScale .yScale

Hi,

I’ve got an object pool setup with a few hundred objects. It’s easier for me to just pull from the pool and then set the .width and .height of an object. Is this worse for performance compared to doing .xScale and .yScale? Is adjusting the width and height actually taking up more memory, or does it basically do the same thing?

Thanks.

Unknown.  You should write up some testbenches to measure these things.

If you need code for memory/fps counting you can grab it from SSK2.

Meters:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/meters/

Source: 

https://github.com/roaminggamer/SSK2/blob/master/ssk2/meters.lua

Cool thanks. I’ll go ahead and do that but was hoping someone from corona would chime in and make this quick and easy for me :slight_smile: Seems like pretty straightforward question. 

Actually, I think you’re going to find the difference is negligible if there is any measurable delta at all.

I would simply do whatever works best for you and fits your needs/style.

I have not run tests but imagine it be faster to set width and height because I imagine the xScale and yScale are just the width and height multiplied by the scale.

Testing shows negligible difference as far as speed.

20:02:28.432 --------------------------- 20:02:29.612 Scaling test ran 10 times in 1159.1 ms. 20:02:30.769 Scaling test ran 10 times in 1157.3 ms. 20:02:31.925 Scaling test ran 10 times in 1154.4 ms. 20:02:33.073 Scaling test ran 10 times in 1155.5 ms. 20:02:34.235 Scaling test ran 10 times in 1157 ms. 20:02:34.235 --------------------------- 20:02:35.383 Sizing test ran 10 times in 1147.9 ms. 20:02:36.533 Sizing test ran 10 times in 1146.6 ms. 20:02:37.685 Sizing test ran 10 times in 1146 ms. 20:02:38.825 Sizing test ran 10 times in 1143.9 ms. 20:02:39.973 Sizing test ran 10 times in 1143.5 ms. 20:02:39.973 ---------------------------

Code here: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/10/whichIsFasterScalingOrWidthHeight.zip

-- ============================================================= display.setStatusBar(display.HiddenStatusBar) io.output():setvbuf("no") -- ============================================================= require "ssk2.loadSSK" \_G.ssk.init( { useExternal = true } ) -- ============================================================= local group local objs local size = math.floor(fullh/60) local cols = 50 local rows = 50 local startX = left + size/2 local startY = top + size/2 local curX = startX local curY = startY local function createGrid() local objs = {} group = display.newGroup() for i = 1, cols do for j = 1, rows do objs[#objs+1] = display.newRect( group, curX, curY, size, size ) curX = curX + size end curX = startX curY = curY + size end return objs end local function scalingTest( numRuns ) local scales = { 1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 } numRuns = numRuns or 1 local scale local startTime = system.getTimer() for iter = 1, numRuns do for i = #scales, 1, -1 do local scale = scales[i] for j = 1, #objs do objs[j].xScale = scale objs[j].yScale = scale end end end local endTime = system.getTimer() local duration = endTime - startTime print("Scaling test ran ", numRuns, " times in ", endTime - startTime, " ms.") return duration end local function sizingTest( numRuns ) local scales = { 1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 } for i = 1, #scales do scales[i] = scales[i] \* size end numRuns = numRuns or 1 local scale local startTime = system.getTimer() for iter = 1, numRuns do for i = #scales, 1, -1 do local scale = scales[i] for j = 1, #objs do objs[j].width = scale objs[j].height = scale end end end local endTime = system.getTimer() local duration = endTime - startTime print("Sizing test ran ", numRuns, " times in ", endTime - startTime, " ms.") return duration end print ("---------------------------") objs = createGrid() scalingTest(10) scalingTest(10) scalingTest(10) scalingTest(10) scalingTest(10) display.remove(group) print ("---------------------------") objs = createGrid() sizingTest(10) sizingTest(10) sizingTest(10) sizingTest(10) sizingTest(10) display.remove(group) print ("---------------------------") --]]

For those who don’t want to read the code, that is a < 10 ms total difference (on average) between the two techniques over a run of 25,000 operations.  

Unknown.  You should write up some testbenches to measure these things.

If you need code for memory/fps counting you can grab it from SSK2.

Meters:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/meters/

Source: 

https://github.com/roaminggamer/SSK2/blob/master/ssk2/meters.lua

Cool thanks. I’ll go ahead and do that but was hoping someone from corona would chime in and make this quick and easy for me :slight_smile: Seems like pretty straightforward question. 

Actually, I think you’re going to find the difference is negligible if there is any measurable delta at all.

I would simply do whatever works best for you and fits your needs/style.

I have not run tests but imagine it be faster to set width and height because I imagine the xScale and yScale are just the width and height multiplied by the scale.

Testing shows negligible difference as far as speed.

20:02:28.432 --------------------------- 20:02:29.612 Scaling test ran 10 times in 1159.1 ms. 20:02:30.769 Scaling test ran 10 times in 1157.3 ms. 20:02:31.925 Scaling test ran 10 times in 1154.4 ms. 20:02:33.073 Scaling test ran 10 times in 1155.5 ms. 20:02:34.235 Scaling test ran 10 times in 1157 ms. 20:02:34.235 --------------------------- 20:02:35.383 Sizing test ran 10 times in 1147.9 ms. 20:02:36.533 Sizing test ran 10 times in 1146.6 ms. 20:02:37.685 Sizing test ran 10 times in 1146 ms. 20:02:38.825 Sizing test ran 10 times in 1143.9 ms. 20:02:39.973 Sizing test ran 10 times in 1143.5 ms. 20:02:39.973 ---------------------------

Code here: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/10/whichIsFasterScalingOrWidthHeight.zip

-- ============================================================= display.setStatusBar(display.HiddenStatusBar) io.output():setvbuf("no") -- ============================================================= require "ssk2.loadSSK" \_G.ssk.init( { useExternal = true } ) -- ============================================================= local group local objs local size = math.floor(fullh/60) local cols = 50 local rows = 50 local startX = left + size/2 local startY = top + size/2 local curX = startX local curY = startY local function createGrid() local objs = {} group = display.newGroup() for i = 1, cols do for j = 1, rows do objs[#objs+1] = display.newRect( group, curX, curY, size, size ) curX = curX + size end curX = startX curY = curY + size end return objs end local function scalingTest( numRuns ) local scales = { 1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 } numRuns = numRuns or 1 local scale local startTime = system.getTimer() for iter = 1, numRuns do for i = #scales, 1, -1 do local scale = scales[i] for j = 1, #objs do objs[j].xScale = scale objs[j].yScale = scale end end end local endTime = system.getTimer() local duration = endTime - startTime print("Scaling test ran ", numRuns, " times in ", endTime - startTime, " ms.") return duration end local function sizingTest( numRuns ) local scales = { 1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 } for i = 1, #scales do scales[i] = scales[i] \* size end numRuns = numRuns or 1 local scale local startTime = system.getTimer() for iter = 1, numRuns do for i = #scales, 1, -1 do local scale = scales[i] for j = 1, #objs do objs[j].width = scale objs[j].height = scale end end end local endTime = system.getTimer() local duration = endTime - startTime print("Sizing test ran ", numRuns, " times in ", endTime - startTime, " ms.") return duration end print ("---------------------------") objs = createGrid() scalingTest(10) scalingTest(10) scalingTest(10) scalingTest(10) scalingTest(10) display.remove(group) print ("---------------------------") objs = createGrid() sizingTest(10) sizingTest(10) sizingTest(10) sizingTest(10) sizingTest(10) display.remove(group) print ("---------------------------") --]]

For those who don’t want to read the code, that is a < 10 ms total difference (on average) between the two techniques over a run of 25,000 operations.