I was wondering about a similar question (maybe I got you wrong, though) so I tried to benchmark it and it turned out no different. Maybe my setup is helpful anyways. Here goes:
--[[
Benchmarking globals (that are transformed to locals) vs macros
timing function borrowed from lume.lua - <a href="https://github.com/rxi/lume" rel="nofollow">https://github.com/rxi/lume</a>
--]]localfunction benchmark(fn, ...)local start =os.clock()local rtn ={fn(...)}return(os.clock() - start), unpack(rtn)endlocalfunction mean(t)local sum =0local count=0for k,v inpairs(t)doiftype(v)=='number'then
sum = sum + v
count = count + 1endendreturn(sum / count)endfunction standardDeviation(t)local m
local vm
local sum =0local count =0local result
m = mean(t)for k,v inpairs(t)doiftype(v)=='number'then
vm = v - m
sum = sum + (vm * vm)
count = count + 1endend
result =math.sqrt(sum /(count-1))return result
end
repeatBenchmarks =1000-- repeat the benchmark n times
resultsLocal ={}
resultsMacro ={}-- global variable
globalVar =10-- macro constant
macroVar @10localfunction localFun()local l = globalVar
local r =0for i =1,100000do
r = r + l
endendlocalfunction macroFun()local r =0for i =1,100000do
r = r + macroVar
endend-- repeat the benchmark a couple of timesfor r =1,repeatBenchmarks dotable.insert(resultsLocal, benchmark(localFun))table.insert(resultsMacro, benchmark(macroFun))endprint("MEAN local: "..mean(resultsLocal))print("SD local: "..standardDeviation(resultsLocal))print("MEAN macro: "..mean(resultsMacro))print("SD macro: "..standardDeviation(resultsMacro))
There is no difference, so I keep using locals... But it might not be what you were looking for... O:-)
In addition to this, macros are currently not global. The scope is limited to the file/class in which they were created. This is a bit of a bother, especially since, ironically, each macro variable name has to be unique - globally.
a will be tokenised to include the search for leftBorder and the addition of 40.
but...
leftBorder@10 a=leftBorder+40
a will be tokenised directy to a=50. This is because the macro text will be converted to 10 and the byte compiler will see 10+40 and convert this as an optimisation to 50.
So if you have a lot of constants in your code then macros could (overall) speed things up.
The other thing is that locals can be in short supply, macros are a good way of saving local 'space'.
Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!). https://deluxepixel.com
Thansk @SinisterSoft. Wow, I can't see how you could run out of locals (limit is 200) unless you made your entire game one giant file (like I used to LOL) b-(
I use locals whenever I can - they are a lot faster than globals.
I tend to have one big game loop that does virtually everything - with a scenemanager overlayed on top controlling the game state machine.
Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!). https://deluxepixel.com
Comments
O:-)
Likes: antix
local leftBorder=10
a=leftBorder+40
a will be tokenised to include the search for leftBorder and the addition of 40.
but...
leftBorder@10
a=leftBorder+40
a will be tokenised directy to a=50. This is because the macro text will be converted to 10 and the byte compiler will see 10+40 and convert this as an optimisation to 50.
So if you have a lot of constants in your code then macros could (overall) speed things up.
The other thing is that locals can be in short supply, macros are a good way of saving local 'space'.
Likes: antix
https://deluxepixel.com
Likes: SinisterSoft
I tend to have one big game loop that does virtually everything - with a scenemanager overlayed on top controlling the game state machine.
https://deluxepixel.com