Module:Sandbox/ProblemSolving/Test

local function split(s) local r = {} for i in string.gmatch(s, '.') do   table.insert(r, i)  end return r end

local solutions = {}

function solutions.s1(s) local r = '' for i = #s, 1, -1 do   r = r .. s:sub(i, i) end return r end

function solutions.s2(s) local r = '' for i = 1, #s do   r = s:sub(i, i) .. r end return r end

function solutions.s3(s) local t = split(s) local r = '' for i = #t, 1, -1 do   r = r .. t[i] end return r end

function solutions.s4(s) local t = split(s) local r = '' for i = 1, #t do   r = t[i] .. r end return r end

function solutions.s5(s) local t = split(s) local r = {} for i = #t, 1, -1 do   table.insert(r, t[i]) end return table.concat(r) end

function solutions.s6(s) local r = {} for i = #s, 1, -1 do   table.insert(r, s:sub(i, i)) end return table.concat(r) end

local function test(name, s, sr, n) s = s or 'Hello World' sr = sr or 'dlroW olleH' n = n or 100000 local fn = solutions[name] if fn(s) ~= sr then mw.log(name, false) return end local t1 = os.clock * 1000000 for i = 1, n do   fn(s) end local t2 = os.clock * 1000000 mw.log(name, true, t2 - t1) end

local long_string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'

local long_string_r = string.reverse(long_string)

return function(long) if long then test('s1', long_string, long_string_r, 10000) test('s2', long_string, long_string_r, 10000) test('s3', long_string, long_string_r, 10000) test('s4', long_string, long_string_r, 10000) test('s5', long_string, long_string_r, 10000) test('s6', long_string, long_string_r, 10000) else test('s1') test('s2') test('s3') test('s4') test('s5') test('s6') end end

-- print(p)

-- s1	true	204869 -- s2	true	165331 -- s3	true	387576 -- s4	true	407139 -- s5	true	664412 -- s6	true	330049

-- print(p(true))

-- s1	true	941767 -- s2	true	907726 -- s3	true	1490418 -- s4	true	1370027 -- s5	true	1507229 -- s6	true	1021922