-->
Page 1 of 1

Measure of exucution time

PostPosted: Fri Apr 07, 2017 5:40 pm
by MaxItaly
Hello !
How i can measure the execution time of some function ? using a tmr ?
Max

Re: Measure of exucution time

PostPosted: Sat Apr 08, 2017 2:09 pm
by marcelstoer
If you need wall-clock time you can use the rtctime module in combination with the SNTP module. If wall-clock isn't relevant tmr.time() may be enough.

Re: Measure of exucution time

PostPosted: Sat Apr 15, 2017 4:58 am
by Frank Buss
You can profile a function like this:
Code: Select allfunction profile(name)
   local start = tmr.now()
   _G[name]()
   local delta = tmr.now() - start
   print(name .. " needs " .. (delta / 1000) .. " ms")
end


Example:

Code: Select allfunction longTime()
   local sum = 0
   for i = 1, 100000, 1 do
      sum = sum + i
   end
   print(sum)
end

> profile("longTime")
5000050000
longTime needs 474.855 ms


Resolution is one microsecond. Note the 32 bit counter wraps from time to time and might then show a really big number :D

Re: Measure of exucution time

PostPosted: Sat Apr 15, 2017 4:06 pm
by marcelstoer
Nice example, thanks.