Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Question about Macros — Gideros Forum

Question about Macros

antixantix Member
edited February 2017 in General questions
I am currently using loads of global variables and when I want to access them I use a line like local globalVar= GLOBALVAR.

If I change to using macros will they be faster than using global vars?

Comments

  • stetsostetso Member
    Accepted Answer
    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>
    --]]
     
    local function benchmark(fn, ...)
    	local start = os.clock()
    	local rtn = {fn(...)}
    	return (os.clock() - start), unpack(rtn)
    end
     
    local function mean(t)
    	local sum = 0
    	local count= 0
    	for k,v in pairs(t) do
    		if type(v) == 'number' then
    			sum = sum + v
    			count = count + 1
    		end
    	end
    	return (sum / count)
    end
     
    function standardDeviation(t)
      local m
      local vm
      local sum = 0
      local count = 0
      local result
      m = mean(t)
      for k,v in pairs(t) do
        if type(v) == 'number' then
          vm = v - m
          sum = sum + (vm * vm)
          count = count + 1
        end
      end
      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 @ 10
     
    local function localFun()
    	local l = globalVar
    	local r = 0
    	for i = 1,100000 do
    		r = r + l
    	end
    end
     
    local function macroFun()
    	local r = 0
    	for i = 1,100000 do
    		r = r + macroVar
    	end
    end
     
    -- repeat the benchmark a couple of times
    for r = 1,repeatBenchmarks do
    	table.insert(resultsLocal, benchmark(localFun))
    	table.insert(resultsMacro, benchmark(macroFun))
    end
     
    print("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:-)
  • totebototebo Member
    edited February 2017
    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.

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Hrmm, then I suppose I will stick with faithful old globals then :D
  • SinisterSoftSinisterSoft Maintainer
    edited February 2017
    Technically they can be faster, eg:

    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

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • 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-(

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • 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
  • I also use locals everywhere I can. I use globals to enable variables to be used across classes though.
Sign In or Register to comment.