milliseconds revisited

Hello all,
There have been a number of posts over the years on getting elapsed time in milliseconds. So, even after reading the relevant posts, I am unsure on the best/proper approach. Should I be using:

  1. os.clock() - startTime – where startTime is an earlier os.clock() call
  2. use os.time()
  3. use ElapsedTime
  4. other

Does ElapsedTime (or any of the others) need to be used within the draw() function, or not…?

Please clarify. In the end, I’d like millisecond accuracy to time various functions, operations, etc.

Thanks!

The accuracy of os.clock() is 0.01 second, if you want get more, you may use socket.gettime(), its accuracy is 1 ms, below is an example:

local socket = require("socket")
local t0 = socket.gettime()
-- do something
local t1 = socket.gettime()
print("used time: "..t1-t0.."ms")

os.clock seems to work OK. Here’s a little example showing the differences. Move the slider to change the delay. The display is the average time.

EDIT: I updated my code to include socket.gettime from @binaryblues. I’m not sure why the times are so different at so delay values.

EDIT: Added touch function to reset the counts without changing the delay.

function setup()
    parameter.integer("delay",2,10,4,reset)
    textMode(CORNER)
    socket = require("socket")
    print("tap screen to reset counters")
    print("move slider to change delay")
end

function reset()
    t1,t2,t3,t4,c1=0,0,0,0,0
end

function draw()
    background(40, 40, 50)
    fill(255)
    
    a1=os.clock()
    a2=os.time()
    a3=ElapsedTime
    a4=socket.gettime()
    
    for z=1,10^delay do
        x=math.sin(z)
    end
    
    b1=os.clock()
    b2=os.time()
    b3=ElapsedTime
    b4=socket.gettime()
    
    t1=t1+(b1-a1)
    t2=t2+(b2-a2)
    t3=t3+(b3-a3)
    t4=t4+(b4-a4)
    c1=c1+1
    
    text("delay     "..10^delay,200,450)
    text("os.clock  "..t1/c1,200,400)
    text("os.time   "..t2/c1,200,350)
    text("elapsed   "..t3/c1,200,300)
    text("socket    "..t4/c1,200,250)
end

function touched(t)
    if t.state==BEGAN then
        reset()
    end
end

ElapsedTime and DeltaTime are updated once per draw cycle. So you can call it wherever you like and it will return the exact same figure, within the same draw cycle. So if you wanted to use it for profiling a piece of code, you’d need to take the measurement the draw cycle after the one where the profiled code ran.

@iam3o5am - I sometimes find that one useful method of testing function speed, is to test how many times you can run the function (and do nothing else at all) before the frame speed drops below 60.

So if you can run your function 20,000 times in a draw cycle of 1/60 second, then the function speed is 1.2 million per second.

This approach can also be used to estimate how much of the processing power available for each draw cycle, is used by your function. For example, if you can need to run your function 1,000 times per cycle, and you know it can be run 20,000 times per cycle on its own, then your function is using 5% of the available processing per cycle.

This is a good way to make sure you don’t run too close to the point where things start slowing down.