Jump to content

Recommended Posts

For each variable reference, LUA will try to first find it in the local table, and if that fails then it looks to the environment table, and then if that fails to the global table.

Each lookup is effectively a hashmap lookup, so for a tight loop having things cached in local will be a good thing to do for performance.

a = 0
for i=0,100000
do
    a = a + math.sin(a)
end
--
local a = 0
local msin = math.sin
for i=0,100000
do
    a = a + msin(a)
end

The bottom one will be done quicker than the top due to the locality lookups.

 

In the case of mods it's good practice to keep variables that aren't to be used outside of the mod local.

Keeps the references in check for garbage collection (memory usage) and does less on the CPU for better performance.

  • Like 3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...