GH*master Expert Cheater Reputation: 8
Joined: 10 Jan 2008 Posts: 159
|
Posted: Sun Jun 18, 2017 2:26 am Post subject: Comparing the execution time of two functions |
|
|
Code: | function NoOptimizeCode()
--[[Presumably not optimized code]]--
end
function OptimizeCode()
--[[Presumably optimized code]]--
end
local countRepeat = 100000 -- add more cycle
local x1 = 0
local x2 = 0
x1 = os.clock()
for i = 1, countRepeat do NoOptimizeCode() end
x2 = os.clock()
for i = 1, countRepeat do OptimizeCode() end
local deltaTimeOptimize = (os.clock() - x2)
local deltaTimeNoOptimize = (x2 - x1)
print(string.format("%.2f\n", deltaTimeNoOptimize/deltaTimeOptimize)) |
Examples:
Code: | function NoOptimizeCode()
if string.match('mov eax, [edx]', 'edx') then
end
end
function OptimizeCode()
if string.find('mov eax, [edx]', 'edx') then
end
end
--> 1.38
|
Code: | local tableA = {'A', 'B', 'C'}
function NoOptimizeCode()
local t = #tableA
for i = 1, t do
end
end
function OptimizeCode()
for i = 1, #tableA do
end
end
--> 1.04
|
Code: | tableA = {'A', 'B', 'C'}
local tableB = {'A', 'B', 'C'}
function NoOptimizeCode()
for i = 1, #tableA do
end
end
function OptimizeCode()
for i = 1, #tableB do
end
end
--> 1.14
|
Code: | function NoOptimizeCode()
tableA = {'A', 'B', 'C'}
end
function OptimizeCode()
local tableB = {'A', 'B', 'C'}
end
--> 1.08
|
Try to compatible for:
string.match (s, pattern [, init])
string.gmatch (s, pattern)
string.gsub (s, pattern, repl [, n])
string.find (s, pattern [, init [, plain]])
And other things that you wanted to know for comparison
|
|