lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mason Mackaman <masondeanm@aol.com> writes:

> So to make a program run as fast as possible should you localize
> everything in _ENV you’re going to use in your program?

I was going to say, "No; only if you're going to use it more than once."
But my empirical studies seem to demonstrate otherwise.  The included
program has three functions, all which use a global sub-function "foo".
The first function uses it directly as a global reference.  The second
stashes it in a local variable and uses it from there.  The third has it
pre-stashed as an upvalue.  In each run, the function is referenced one
more time than in the previous run.  The results look like they speak
for themselves.  I tried to be careful in my methodology, but I wouldn't
be surprised if I didn't account for something in my experiment.  Feel
free to attempt to replicate my results (numbers are number of seconds):

1	global = 30	local = 23	upvalue = 22
2	global = 29	local = 23	upvalue = 22
3	global = 29	local = 23	upvalue = 22
4	global = 30	local = 23	upvalue = 22
5	global = 29	local = 23	upvalue = 22
6	global = 30	local = 23	upvalue = 22
7	global = 29	local = 23	upvalue = 22
8	global = 29	local = 23	upvalue = 22
9	global = 29	local = 23	upvalue = 22
10	global = 30	local = 23	upvalue = 22

function foo(a, b)
   return a + b
end

-- uses global foo
local function bar1(n)
   local total = 0
   for i = 1, n do
      total = total + foo(1, 1)
   end
   return total
end

-- uses local foo
local function bar2(n)
   local total = 0
   local foo = foo
   for i = 1, n do
      total = total + foo(1, 1)
   end
   return total
end

-- uses upvalue foo
local fooup = foo
local function bar3(n)
   local total = 0
   for i = 1, n do
      total = total + fooup(1, 1)
   end
   return total
end

local tests = {{"global", bar1}, {"local", bar2}, {"upvalue", bar3}}
local repeats = 20000

collectgarbage("stop")
for i = 1, 10 do
   local out = {tostring(i)}
   for i, v in ipairs(tests) do
      local stime, etime, diff
      local fn = v[2]
      stime = os.time()
      for i = 1, repeats do
         fn(i)
      end
      etime = os.time()
      diff= os.difftime(etime, stime)
      out[i + 1] = v[1] .. " = " .. diff
      collectgarbage()
   end
   print (table.unpack(out))
end


-- 
Michael Welsh Duggan
(mwd@cert.org)