Jump to content

Module:Sandbox/Wnt

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 00:16, 8 July 2014. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- The entire meat of the module can be up front with fields of some table that isn't p.
local q = {}

function round2 (n, decimals)
    local multiplier = decimals ~= nil and 10^decimals or 1 
    return math.floor( n * multiplier + 0.5 ) / multiplier
end
    	
function round1( n, decimals )
    if decimals then
        local multiplier = 10^decimals -- still need this to avoid expensive exponentiation twice
        return math.floor( n * multiplier + 0.5) / multiplier
    else
        return math.floor( n + 0.5)
    end 
end

function q.main (action, args)
        local start = os.clock()
        for i = 1, 1000000 do
           local xxx = round1(13.75)
        end
        local xx = os.clock() - start
        local start = os.clock()
        for i = 1, 1000000 do
        	local xxx = round2(13.75)
        end
        local yy = os.clock() - start
        local xxx = tostring(xx) .. "," .. tostring(yy) .. "; ratio" .. tostring(xx/yy)
	return xxx .. "action = " .. (action or 'NIL') .. "args[1] = " .. (args[1] or 'NIL')
end

-- this final act returns p with the available functions set.  The setmetatable has to be after q is created and q.main defined
local p = {} -- this empty table with metatable serves to convert the first #invoke field to an effective parameter
setmetatable(p, { __index = function(t, k)
	return function(frame)
	    return q.main (k, frame.args)
	end
end })
return p