timer - timing a function / tasks in lua -


i'm trying determine how long statements take run in lua code. code looks this:

function test(self)     local timer1     local timer2     local timer3     timer1 = os.time()     print('timer1 start time is:'.. timer1)     --do stuff.      print( 'timer1 end time is:' , os.difftime(os.time(), timer1) )     timer2 = os.time()     print('timer2 start time is:'.. timer2)      -- lot of stuff     print( 'timer2 end time is:' , os.difftime(os.time(), timer2) )      timer3=os.time()     print('timer3 start time is:'.. timer3)      -- lot of processing...       print( 'timer3 end time is:' , os.difftime(os.time(), timer3) ) end              

this output looks like:

timer1 start time is:1401798084 timer1 end time is: = 0 timer2 start time is:1401798084 timer2 end time is: = 0 timer3 start time is:1401798084 timer3 end time is: = 2 

other things i've tried:

lua - current time in milliseconds

in above post, found snippet of code:

local x = os.clock() local s = 0 i=1,100000 s = s + end os.execute("sleep "..1) print(string.format("elapsed time: %.2f\n", os.clock() - x)) 

i added sleep time... when runs, output:

elapsed time: 0.00 

i'm doing wrong. if have suggestions on how can fix / improve this, i'm ears. in interim, i'm going revisit lua site read on os.difftime() in case i'm using incorrectly.

edit 1

i changed test code this:

local x = os.clock() local s = 0 i=1,100000      s = s +      os.execute("sleep "..1) end print(string.format("elapsed time: %.2f\n", os.clock() - x)) 

and i'm getting values make sense!

os.clock measures cpu time, not wall time. cpu time not include time spent in sleep. script below still prints 0 elapsed time:

local x = os.clock() os.execute("sleep 60") print(string.format("elapsed time: %.2f\n", os.clock() - x)) 

when move os.execute loop, you're measuring time fork shell. script below print nonzero elapsed time, if short loop:

local x = os.clock() i=1,1000 os.execute("true") end print(string.format("elapsed time: %.2f\n", os.clock() - x)) 

finally, got 0 elapsed time in first loop because lua fast. try changing limit 1000000:

local x = os.clock() local s = 0 i=1,1000000 s = s + end print(string.format("elapsed time: %.2f\n", os.clock() - x)) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -