Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Comparing the execution time of two functions

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
GH*master
Expert Cheater
Reputation: 8

Joined: 10 Jan 2008
Posts: 159

PostPosted: Sun Jun 18, 2017 2:26 am    Post subject: Comparing the execution time of two functions Reply with quote

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
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Sun Jun 18, 2017 2:47 am    Post subject: Reply with quote

If you have access to LuaSocket you can use its 'gettime' function for a more precise time than os.clock gives.

Code:
socket.gettime()

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Wed Jun 21, 2017 12:26 pm    Post subject: Reply with quote

getTickCount() can be used as well
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites