This module was made as a sandbox for the user Dessamator. This documentation is kept to prevent redlinks.
-- //Tests performance of a units
-- http://www.lua.org/gems/sample.pdf
local perftest = {}
function perftest.run(testA,testB)
local cpuRunTime = {0,0}
local elapsedTime = {0,0}
if not(testA) or not(testB) then
return "One function is nil"
end
local nClock = os.clock()
testA()
cpuRunTime[1]= os.clock()-nClock
local nClock = os.clock()
testB()
cpuRunTime[2] = os.clock()-nClock
local runTimeText = "Cpu Time 1 : " .. string.sub(cpuRunTime[1],1,4) ..
"\nCpu Time 2 : " ..string.sub(cpuRunTime[2],1,4)
mw.log(runTimeText)
return runTimeText
end
--PatternMatching
function test1()
local s = "the quick brown fox jumps over the lazy dog"
local w = "foo"
local strPat = ".*()" .. w
for i=1,99999 do s:find(strPat) end
end
function test2()
local s = "the quick brown fox jumps over the lazy dog"
local w = "foo"
local strPat = ".*()" .. w
for i = 1,99999 do s:find(strPat) end
end
-- local vs global functions functions
local function a(n)
return n + 1
end
local function b(n)
return a(n)
end
function globalA(n)
return n + 1
end
function globalB(n)
return globalA(n)
end
local function test3()
for c = 1, 1000000 do
b(c)
end
end
local function test4()
for c = 1, 1000000 do
globalB(c)
end
end
local function test5()
local function e(n)
return n+1
end
local function d(n)
return e(n)
end
for c = 1, 1000000 do
d(c)
end
end
---
--Tables initialization
local function test6()
for i = 1, 100000 do
local a = {true, true, true}
a[1] = 1; a[2] = 2; a[3] = 3
end
end
local function test7()
for i = 1, 100000 do
local a = {}
a[1] = 1; a[2] = 2; a[3] = 3
end
end
--
function perftest.runtests(frame)
if not (frame) or not(frame.args) or not(frame.args[1]) then
return "invalid args"
end
local testType = frame.args[1]
if (testType=="1") then
return perftest.run(test1,test2)
elseif (testType=="2") then
return perftest.run(test3,test4)
elseif (testType=="3") then
return perftest.run(test6,test7)
elseif (testType=="4") then
return perftest.run(test4,test5)
end
end
return perftest