Since we have some programming wizards at our midst, I thought to ask you guys for answers rather than trying to guess.
I’ve done a lot of optimisation in my days, and granted, in most cases this level of optimisation would be completely unnecessary.
However, I am not sure why method 1, see below, is around 2 to 2.5 times faster than method 2, see below. I would have expected that, since method 2’s function X contains function Y that it’d be at least as fast if not faster than method 1, where both functions are separate.
What is the reason behind method 1 being significantly faster than method 2? Is it some compiler optimisation?
-- METHOD 1: local function Y() return true end local function X() Y() return true end local getTimer = system.getTimer local startTime = getTimer() local iterations = 1000000 for i = 1, iterations do X() end print( "TIME:"..(getTimer()-startTime) )
-- METHOD 2: local function X() local function Y() return true end Y() return true end local getTimer = system.getTimer local startTime = getTimer() local iterations = 1000000 for i = 1, iterations do X() end print( "TIME:"..(getTimer()-startTime) )