เอกสารการใช้งานสำหรับมอดูลนี้อาจสร้างขึ้นที่ มอดูล:User:Ans/PerformanceTest/doc

local p = {}

-- For passed as named template parameter that is going to be preprocessed.
-- Since input text is assumed to has already been preprocessed, so '{', '}', and '<' are escaped
-- to prevent them to be preprocessed.  '[' is escaped to handle the case like '{{t1|[[a}}
--
--    Example: frame:preprocess('{{t1|a1='..WrapPreprocessedTemplateParameter(s)..'}}')
--
-- Note: This does not convert '=' to '{{=}}'
--
local function WrapPreprocessedTemplateParameter(s)
	return s:gsub('[{}<[]', '%1<!---->'):gsub('|', '{{!}}')
end
local escapeNamedTemplateParameter = WrapPreprocessedTemplateParameter

-- Suitable for passed as unnamed template parameter that is going to be preprocessed,
-- as it also convert '=' to '{{=}}'
--
--    Example: frame:preprocess('{{t1|'..WrapPreprocessedTemplateParameter2(s)..'}}')
--
local function WrapPreprocessedTemplateParameter2(s)
	return s:gsub('[{}<[]', '%1<!---->'):gsub('[|=]', {['|']='{{!}}', ['=']='{{=}}'})
end
local escapeTemplateParameter = WrapPreprocessedTemplateParameter2

local function WrapPreprocessedTemplateParameter3(s)
	return string.gsub(string.gsub(s, '[{}<[]', '%1<!---->'), '[|=]', {['|']='{{!}}', ['=']='{{=}}'})
end

local gsub = string.gsub
local function WrapPreprocessedTemplateParameter4(s)
	return gsub(gsub(s, '[{}<[]', '%1<!---->'), '[|=]', {['|']='{{!}}', ['=']='{{=}}'})
end

-- WrapPreprocessedTemplateParameter[56]() is 20-50% slower than mw.text.nowiki()/nowiki()
local function WrapPreprocessedTemplateParameter5(s)
	return mw.ustring.gsub(mw.ustring.gsub(s, '[{}<[]', '%1<!---->'), '[|=]', {['|']='{{!}}', ['=']='{{=}}'})
end

local ugsub = mw.ustring.gsub
local function WrapPreprocessedTemplateParameter6(s)
	return ugsub(ugsub(s, '[{}<[]', '%1<!---->'), '[|=]', {['|']='{{!}}', ['=']='{{=}}'})
end

local function s(s)
	return s
end

local a = 'ก'
local m = '๕๑'
local d = '๑'

-- This make preprocessNowiki2M() 15-20% faster than preprocessNowikiM(),
-- but it is still 80-100% slower than preprocessWrapM(), don't know why.
-- The profiling data show that most extra time has been taken by function named '?'.
local nowiki = mw.text.nowiki

function p.preprocess(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{ชว}}{{กม|วล|'..a..'}}')
	end
end
function p.preprocessNowiki(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{ชว}}{{กม|วล|'..mw.text.nowiki(a)..'}}')
	end
end
function p.preprocessNowiki2(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{ชว}}{{กม|วล|'..nowiki(a)..'}}')
	end
end
function p.preprocessWrap(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{ชว}}{{กม|วล|'..WrapPreprocessedTemplateParameter2(a)..'}}')
	end
end
function p.expandTemplate(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..
			frame:expandTemplate{title='ชว', args={}}..
			frame:expandTemplate{title='กม', args={'วล', a}}
	end
end

function p.preprocessM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|ม|'..m..'}}')
	end
end
function p.preprocessNowikiM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|ม|'..mw.text.nowiki(m)..'}}')
	end
end
function p.preprocessNowiki2M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|ม|'..nowiki(m)..'}}')
	end
end
function p.preprocessWrapM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|ม|'..WrapPreprocessedTemplateParameter2(m)..'}}')
	end
end
function p.expandTemplateM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:expandTemplate{title='กม', args={'ม', m}}
	end
end
function p.nowikiM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..mw.text.nowiki(m)
	end
end
function p.nowiki2M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..nowiki(m)
	end
end
function p.wrap2M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..WrapPreprocessedTemplateParameter2(m)
	end
end
function p.wrap3M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..WrapPreprocessedTemplateParameter3(m)
	end
end
function p.wrap4M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..WrapPreprocessedTemplateParameter4(m)
	end
end
function p.wrap5M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..WrapPreprocessedTemplateParameter5(m)
	end
end
function p.wrap6M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..WrapPreprocessedTemplateParameter6(m)
	end
end
-- This take time 9-10% of wrap2M()
function p.sM(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..s(m)
	end
end
-- This take time 5-6% of wrap2M()
function p.M(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..m
	end
end
function p._(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..''
	end
end
function p.__(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p
	end
end

function p.preprocessD(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|วล|'..d..'}}')
	end
end
function p.preprocessNowikiD(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|วล|'..mw.text.nowiki(d)..'}}')
	end
end
function p.preprocessNowiki2D(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|วล|'..nowiki(d)..'}}')
	end
end
function p.preprocessWrapD(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:preprocess('{{กม|วล|'..WrapPreprocessedTemplateParameter2(d)..'}}')
	end
end
function p.expandTemplateD(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	for i = 1, n do
		r = p..frame:expandTemplate{title='กม', args={'วล', d}}
	end
end

-- notTrue2/notFalse2 is 65-75% slower than notTrue/notFalse
-- notTrue2 is 27.56% slower than notTrue3
-- notNil2 is 93.03% slower than notNil
function p.notTrue(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = true
	for i = 1, n do
		if not a then r = p end
	end
end
function p.notTrue2(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = true
	for i = 1, n do
		if a == false then r = p end
	end
end
function p.notTrue3(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = true
	for i = 1, n do
		if a == nil then r = p end
	end
end
function p.notFalse(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = false
	for i = 1, n do
		if not a then r = p end
	end
end
function p.notFalse2(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = false
	for i = 1, n do
		if a == false then r = p end
	end
end
function p.notNil(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = nil
	for i = 1, n do
		if not a then r = p end
	end
end
function p.notNil2(frame)
	local n = tonumber(frame.args[1])
	local r, p = '', ''
	local a = nil
	for i = 1, n do
		if a == nil then r = p end
	end
end

return p