Jump to content




Timing a function?


  • You cannot reply to this topic
2 replies to this topic

#1 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

Posted 08 June 2015 - 05:17 PM

This might be off topic, as I know how to do this in computercraft but my question is about vanilla lua. I have a function and I want to see how long it takes to execute (To the millisecond)
This is my current code:
function Test_Function()
    for i = 1, 100 do
        local s = math.log( i )
        s = math.sqrt( s )
    end
end

t1 = os.clock()
    Test_Function()
t2 = os.clock()

print( os.difftime(t2, t1) )
However this is returning the amount of seconds it takes, not the amount of milliseconds. Is there a way to measure how long a function takes to executes to the millisecond in vanilla lua?

#2 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 08 June 2015 - 05:22 PM

I don't know of any without extending libraries.

I would run the test 20x and devide the amount of seconds taken by 20, then you should have the same accuracy as ingame

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 June 2015 - 12:24 AM

I doubt you'll get any more accuracy than os.clock() gives you. After all, if the environment you were using had better, it'd be assigned to os.clock(), yeah?

But I suspect os.difftime() is doing some rounding. What happens if you simply do print(t2 - t1)?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users