Module:Timing Explained

-- module timing individual functions-- @license (CC-BY-SA-3.0)-- @copyright John Erling Blad

-- @var The table holding this modules exported memberslocal p =

-- access function for the number of items in each sets-- @param number new count of items in each set-- @return number of items in each setfunction p.count(num) if num then assert(num>0, "Value of 'num' can\'t be zero") p._count = num end return p._countend

-- access function for the number of sets-- @param number new count of sets-- @return number of setsfunction p.sets(num) if num then assert(num>0, "Value of 'sets' can\'t be zero") p._sets = num end return p._setsend

-- calculate the statistics for time series, and report mean and variance-- for some background on this calculation, see and -- @param table timing is a sequence of time differences-- @return table of mean and variancefunction p.stats(timing) local minVal = timing[1] local maxVal = timing[1] local sqr,mean=0,0 for i,v in ipairs(timing) do minVal = v < minVal and v or minVal maxVal = v > maxVal and v or maxVal mean = v + mean sqr = math.pow(v,2) + sqr end mean = mean / #timing local var = (sqr / #timing) - math.pow(mean,2) return end

-- runner that iterates a provided function while taking the time for each chunk of iterations-- @param function func that is the kernel of the iterations-- @return table of runtime for the given functionfunction p.runner(func, ...) -- measure the function local time = for i=1,p._sets do time[i] = os.clock for j=1,p._count do func(...) end time[i] = os.clock - time[i] end assert(#time>0, "No entries for time measurements") return timeend

-- combine the measurements into a new form-- for some background on this calculation, see -- @param table tick stats for the baseline-- @param table tack stats for the observed function-- @return table with the combined stats, shifted from variance to standard deviationfunction p.combine(tick, tack) -- adjust the actual measurement for the baseline return end

-- formatter for the result produced by the runner-- @param table timing as a mean and a standard deviation-- @return string describing the resultfunction p.report(timing) local messages = messages['call-result'] = 'Each call was running for about $1 seconds.\n' messages['mean-result'] = '\tMean runtime for each set was $1 seconds,\n\twith standard deviation of $2 seconds,\n\tminimum $3, maximum $4.\n' messages['overall-result'] = '\tTotal time spent was about $1 seconds.\n' messages['load-result'] = 'Relative load is estimated to $1.\n' local function g(key, ...) local msg = mw.message.new('timing-' .. key) if msg:isBlank then msg = mw.message.newRawMessage(messages[key]) end return msg end local function f(formatstring, nums) local formatted = for _,v in ipairs(nums) do formatted[1+#formatted] = string.format(formatstring, v) end return formatted end local strings = return table.concat(strings, )end

-- a dummy function that is used for a baseline measure-- @return nilfunction p.nop return nilend

-- metatable for the exportlocal mt =

-- install the metatablesetmetatable(p, mt)

-- donereturn p