This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Timing a function?

Started by mrpoopy345, 08 June 2015 - 04:17 PM
mrpoopy345 #1
Posted 08 June 2015 - 06: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?
InDieTasten #2
Posted 08 June 2015 - 06: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
Bomb Bloke #3
Posted 09 June 2015 - 01: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)?