<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ATiming</id>
	<title>Module:Timing - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ATiming"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Timing&amp;action=history"/>
	<updated>2026-04-22T08:54:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Timing&amp;diff=147272&amp;oldid=prev</id>
		<title>imported&gt;Jeblad at 20:12, 28 May 2016</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Timing&amp;diff=147272&amp;oldid=prev"/>
		<updated>2016-05-28T20:12:03Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- module timing individual functions&lt;br /&gt;
-- @license (CC-BY-SA-3.0)&lt;br /&gt;
-- @copyright John Erling Blad &amp;lt;jeblad@gmail.com&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-- @var The table holding this modules exported members&lt;br /&gt;
local p = {&lt;br /&gt;
	[&amp;#039;_count&amp;#039;] = 100,&lt;br /&gt;
	[&amp;#039;_sets&amp;#039;] = 10,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- access function for the number of items in each sets&lt;br /&gt;
-- @param number new count of items in each set&lt;br /&gt;
-- @return number of items in each set&lt;br /&gt;
function p.count( num )&lt;br /&gt;
	if num then&lt;br /&gt;
		assert(num&amp;gt;0, &amp;quot;Value of &amp;#039;num&amp;#039; can\&amp;#039;t be zero&amp;quot;)&lt;br /&gt;
		p._count = num&lt;br /&gt;
	end&lt;br /&gt;
	return p._count&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- access function for the number of sets&lt;br /&gt;
-- @param number new count of sets&lt;br /&gt;
-- @return number of sets&lt;br /&gt;
function p.sets( num )&lt;br /&gt;
	if num then&lt;br /&gt;
		assert(num&amp;gt;0, &amp;quot;Value of &amp;#039;sets&amp;#039; can\&amp;#039;t be zero&amp;quot;)&lt;br /&gt;
		p._sets = num&lt;br /&gt;
	end&lt;br /&gt;
	return p._sets&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- calculate the statistics for time series, and report mean and variance&lt;br /&gt;
-- for some background on this calculation, see [[w:en:average]] and [[w:en:Standard deviation]]&lt;br /&gt;
-- @param table timing is a sequence of time differences&lt;br /&gt;
-- @return table of mean and variance&lt;br /&gt;
function p.stats( timing )&lt;br /&gt;
	local minVal = timing[1]&lt;br /&gt;
	local maxVal = timing[1]&lt;br /&gt;
	local sqr,mean=0,0&lt;br /&gt;
	for i,v in ipairs(timing) do&lt;br /&gt;
		minVal = v &amp;lt; minVal and v or minVal&lt;br /&gt;
		maxVal = v &amp;gt; maxVal and v or maxVal&lt;br /&gt;
		mean = v + mean&lt;br /&gt;
		sqr = math.pow(v,2) + sqr&lt;br /&gt;
	end&lt;br /&gt;
	mean = mean / #timing&lt;br /&gt;
	local var = (sqr / #timing) - math.pow(mean,2)&lt;br /&gt;
	return { mean, var, minVal, maxVal }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- runner that iterates a provided function while taking the time for each chunk of iterations&lt;br /&gt;
-- @param function func that is the kernel of the iterations&lt;br /&gt;
-- @return table of runtime for the given function&lt;br /&gt;
function p.runner(func, ...)&lt;br /&gt;
	-- measure the function&lt;br /&gt;
	local time = {}&lt;br /&gt;
	for i=1,p._sets do&lt;br /&gt;
		time[i] = os.clock()&lt;br /&gt;
		for j=1,p._count do&lt;br /&gt;
			func(...)&lt;br /&gt;
		end&lt;br /&gt;
		time[i] = os.clock() - time[i]&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	assert(#time&amp;gt;0, &amp;quot;No entries for time measurements&amp;quot;)&lt;br /&gt;
	&lt;br /&gt;
	return time&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- combine the measurements into a new form&lt;br /&gt;
-- for some background on this calculation, see [[w:en:Sum of normally distributed random variables]]&lt;br /&gt;
-- @param table tick stats for the baseline&lt;br /&gt;
-- @param table tack stats for the observed function&lt;br /&gt;
-- @return table with the combined stats, shifted from variance to standard deviation&lt;br /&gt;
function p.combine(tick, tack)&lt;br /&gt;
	-- adjust the actual measurement for the baseline&lt;br /&gt;
	return { tack[1] - tick[1], math.pow(tack[2] + tick[2], 0.5), tick[3], tick[4] }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- formatter for the result produced by the runner&lt;br /&gt;
-- @param table timing as a mean and a standard deviation&lt;br /&gt;
-- @return string describing the result&lt;br /&gt;
function p.report( timing )&lt;br /&gt;
	local messages = {}&lt;br /&gt;
	messages[&amp;#039;call-result&amp;#039;] = &amp;#039;Each call was running for about $1 seconds.\n&amp;#039;&lt;br /&gt;
	messages[&amp;#039;mean-result&amp;#039;] = &amp;#039;\tMean runtime for each set was $1 seconds,\n\twith standard deviation of $2 seconds,\n\tminimum $3, maximum $4.\n&amp;#039;&lt;br /&gt;
	messages[&amp;#039;overall-result&amp;#039;] = &amp;#039;\tTotal time spent was about $1 seconds.\n&amp;#039;&lt;br /&gt;
	messages[&amp;#039;load-result&amp;#039;] = &amp;#039;Relative load is estimated to $1.\n&amp;#039;&lt;br /&gt;
	&lt;br /&gt;
	local function g( key, ...)&lt;br /&gt;
		local msg = mw.message.new( &amp;#039;timing-&amp;#039; .. key )&lt;br /&gt;
		if msg:isBlank() then&lt;br /&gt;
			msg = mw.message.newRawMessage( messages[key] )&lt;br /&gt;
		end&lt;br /&gt;
		return msg&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function f(formatstring, nums)&lt;br /&gt;
		local formatted = {}&lt;br /&gt;
		for _,v in ipairs(nums) do&lt;br /&gt;
			formatted[1+#formatted] = string.format( formatstring, v )&lt;br /&gt;
		end&lt;br /&gt;
		return formatted&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local strings = {&lt;br /&gt;
		g(&amp;#039;call-result&amp;#039;):numParams(unpack(f(&amp;#039;%.1e&amp;#039;, timing[1]))):plain(),&lt;br /&gt;
		g(&amp;#039;mean-result&amp;#039;):numParams(unpack(f(&amp;#039;%.1e&amp;#039;, timing[2]))):plain(),&lt;br /&gt;
		g(&amp;#039;overall-result&amp;#039;):numParams(unpack(f(&amp;#039;%.1e&amp;#039;, timing[3]))):plain(),&lt;br /&gt;
		g(&amp;#039;load-result&amp;#039;):numParams(unpack(f(&amp;#039;%.1f&amp;#039;, timing[4]))):plain()&lt;br /&gt;
	}&lt;br /&gt;
	return table.concat(strings, &amp;#039;&amp;#039;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- a dummy function that is used for a baseline measure&lt;br /&gt;
-- @return nil&lt;br /&gt;
function p.nop()&lt;br /&gt;
	return nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- metatable for the export&lt;br /&gt;
local mt = {&lt;br /&gt;
	-- call-form of the table, with arguments as if it were runner()&lt;br /&gt;
	-- @paramfunction func that is the kernel of the iterations&lt;br /&gt;
	-- @return string describing the result&lt;br /&gt;
	__call = function (self, func, ...)&lt;br /&gt;
		-- resolve to the same level&lt;br /&gt;
		local f1 = p.nop&lt;br /&gt;
		local f2 = func&lt;br /&gt;
		&lt;br /&gt;
		-- start the clock&lt;br /&gt;
		local tick = os.clock()&lt;br /&gt;
	&lt;br /&gt;
		local baseline = self.stats( self.runner(f1, ...) )&lt;br /&gt;
		local actual = self.stats( self.runner(f2, ...) )&lt;br /&gt;
		&lt;br /&gt;
		local combined = self.combine(baseline, actual)&lt;br /&gt;
		local tack = os.clock()&lt;br /&gt;
		return self.report({{ combined[1] / p._count }, combined, { tack - tick }, {actual[1]/baseline[1]}})&lt;br /&gt;
	end&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- install the metatable&lt;br /&gt;
setmetatable(p, mt)&lt;br /&gt;
&lt;br /&gt;
-- done&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Jeblad</name></author>
	</entry>
</feed>