So I’ve been doing some reading on how to make code more performant, and I came across this:
for i = 0, 100000 do
math.sin(i)
end
--is slower than
local sin = math.sin
for i = 0, 100000 do
sin(i)
end
I’m working on something that requires use of a very large variety of math functions, which are called very frequently. However I don’t really feel like localizing every single one, so my question is, could the code below be a way of localizing the whole math library? If not how could I do this?
local real = math
local math = setmetatable({}, {
__index = function(t, k)
if real[k] then
t[k] = real[k]
return real[k]
end
end
})