View Single Post
04-18-09, 02:16 AM   #43
Kemayo
A Cyclonian
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 41
I agree with the "you're doing something wrong in an OnUpdate if you need that many variables" consensus.

That said, if you insist on doing it, this would work:

Code:
local timeit = function(f, n)
    local start = os.clock()
    for i=0, n do
        f()
    end 
    print("Ran "..n.." times in "..(os.clock() - start).." seconds")
end

local store = {}
local function wipe(t)
    for k,v in pairs(t) do
        t[k] = nil
    end 
end
local function pack(t, ...)
    wipe(store)
    for i=1, select('#', ...) do
        table.insert(t, (select(i, ...)))
    end 
end
function Foo_OnUpdate_A()
    local a, b, c, d, e = unpack(store)
    if not a then
        a, b, c, d, e = 1, 2, 3, 4, 5
    end 
    -- do stuff
    pack(store, a, b, c, d, e)
end

timeit(Foo_OnUpdate_A, 100000) -- prints "Ran 100000 times in 0.54 seconds" on my computer...
The overhead from using tables is not large. I included the cute benchmarker to demonstrate that.

(And the code presented is less efficient than in-WoW code would be, because it could use the presumably optimized built-in wipe function instead of the one I wrote there.)

EDIT: To be fair, I increased the number of variables being stored. With 26 variables, it runs 100,000 times in 2.26 seconds.

Last edited by Kemayo : 04-18-09 at 02:22 AM.