skittles sour Posted July 5, 2021 Share Posted July 5, 2021 Hi so Lua manuals say that local variables enjoy faster access. How much faster is this access? Besides this advantage, and the advantage of not cluttering the global environment, what other advantages does the local variable enjoy? Link to comment https://forums.kleientertainment.com/forums/topic/131579-advantages-of-local-variables/ Share on other sites More sharing options...
CarlZalph Posted July 8, 2021 Share Posted July 8, 2021 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. 3 Link to comment https://forums.kleientertainment.com/forums/topic/131579-advantages-of-local-variables/#findComment-1477134 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now