Does it make sense to make global functions local?

Hello. I’m worried about this question - does it make sense to make global functions local? Will it give a performance boost when called in hooks or timers, or does it make no sense in GLUA?

Rough example:
Code_i2gwk8E2sh

Perhaps the example is not the best, but it reflects the essence of the question.

I believe it does, but they are very micro optimisations and I dont think its worth sacrificing the cleanliness of your code for it.

Edit: also it would probably be better to do local file_read = file.Read instead of localising the whole file table.

1 Like

This means that it makes sense to do this in methods that can be called every tick or in large loops, otherwise the gain will not be very noticeable. Okay, thanks. :slightly_smiling_face:

Absolutely not, it makes no sense at all, I suppose I should explain why because I saw this crap in many scripts. Many believe that it boost performance, but programming is not about believing. If you aren’t certain about it, you can simply write a test, so here it is

local function benchmarkCall(func)
    local startTime = SysTime()
    func()
    return SysTime() - startTime
end

local iterations = 1000000000
local number = 3.456789
local math_abs = math.abs

print(
    "math.abs using global declaration: ",
    1000 * benchmarkCall(function ()
        for i = 1, iterations do
            number = math.abs(number)
        end
    end),
    "ms"
)

print(
    "math.abs using local declaration: ",
    1000 * benchmarkCall(function ()
        for i = 1, iterations do
            number = math_abs(number)
        end
    end),
    "ms"
)

and here are my results

math.abs using global declaration: 	266.03539999996	ms
math.abs using local declaration: 	264.18299999978	ms

math.abs using global declaration: 	264.0064000002	ms
math.abs using local declaration: 	264.26460000039	ms

math.abs using global declaration: 	268.50609999974	ms
math.abs using local declaration: 	264.80240000001	ms

math.abs using global declaration: 	264.1580999998	ms
math.abs using local declaration: 	264.33589999988	ms

math.abs using global declaration: 	275.89799999987	ms
math.abs using local declaration: 	263.89400000016	ms

math.abs using global declaration: 	263.67359999995	ms
math.abs using local declaration: 	263.64570000032	ms

math.abs using global declaration: 	265.84260000027	ms
math.abs using local declaration: 	263.67530000016	ms

As you can see sometimes local declaration is faster, sometimes not, but there is no performance loss to worry about.

I recommend you to care more about improving readability of your code at first rather than optimization, the last one is only necessary when you face problems with performance.

While I wrote this post I found one case with unobvious performance drop, it can be something with context switching inside Lua argument stack

for i = 1, iterations do
    math.abs(number)
end

for i = 1, iterations do
    math_abs(number)
end
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
math.abs using global declaration: 	2269.1199000001	ms
math.abs using local declaration: 	275.08339999986	ms

math.abs using global declaration: 	2280.8823	ms
math.abs using local declaration: 	263.92000000033	ms

Conclusion: you should test optimization tricks in real cases and apply them only if necessary.

1 Like