Question about Performance

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
})

No. The speed boost is because it is a local variable instead of a table lookup. A metatable index lookup would almost certainly be slower than either.

Localizing library functions is usually not necessary, and is done as a last steps micro-optimization.

Basically no difference at all in the grand sceme of things