milliseconds

hi, i need to know milliseconds between 2 events. i read a few threads in the forum but there is no clear answer. it seems that the time functions might return better precision than whole seconds but not sure and not documented. it also looks like the app runs 30fps (draw() is run 30x/s). if that’s the case and is really constant (not dropping frames), i guess i could use that while a full millis() function is implemented. can you confirm on both fronts (time funcion precision and fps)?
thanks much - loving codea so far!

Hi tateu,

AFAIK both the EllapsedTime() and DeltaTime() functions do return better precision than whole seconds.

The one issue with this is that the ElapsedTime and DeltaTime values are only updated once per frame. So they won’t measure any differences that occur within a single frame.

os.clock() will allow you to measure long operations with sub-second accuracy. For example:

startTime = os.clock()

v = vec2(0,0)

for i=1,10000 do
    v = v + vec2(1,1)
end

print("Time taken: " .. os.clock() - startTime)

I think draw() runs at 60 fps.

thanks all.
@simeon. so i guess i can just try your code, but what is the unit of the number printed?
and since it seems i won’t get better precision than a frame, can you confirm draw runs at 60fps as fred suggests? so result will be a multiple of 16.67ms?
thanks!

hi again, i just tried your code simeon and i don’t get anything steady. it seems draw does not loop at a regular interval but rather executes whatever code is in it and loops after that. if you change the max value of i to 100 or 100000, you don’t get the same “time”. even if you assume staying at the same “schedule”, it does not seem to be stable. a time measure of the same loop returns anything between .007 and .017, quite a discrepancy if you base your code on time.
it would be great to have either access to real milliseconds or at least a for sure 60fps no latter what code is in draw.
thanks for the help.

Draw will run at 60fps in optimal conditions. If you do something heavy in draw() it will slow down the frame-rate and frames will no longer take a consistent amount of time to draw.

Both ElapsedTime and os.clock() return seconds with fractional components. ElapsedTime should go down to millisecond accuracy.

On an ipad1 with the speed that frames get called, the distance between .001 second increments is a bit inconsistent. I haven’t been able to narrow it down to the order the slices are called in, some binary decimal conversion, or the random slice of a second the timers get started in.

Basically you can get short slices at .001 increments but they may not be even.

.01 (which is a long time, and you should really only ever use one in a draw loop) is consistent.

Code example



--# Count
Count = class()

function Count:init(name,wait)
    self.name = name
    self.i = 0
    self.wait = wait
end

function Count:count()
    local startTime = os.clock()
    while os.clock() - startTime < self.wait do
        self.i = self.i + 1
    end
end

function Count:draw(x,y)
    text(self.name .. ":" .. self.i,x,y)
end
--# Main

function setup()
    print("Hello World!")
    c6 = Count("c .006",.006)
    c5 = Count("c .005",.005)
    c4 = Count("c .004",.004)
    c3 = Count("c .003",.003)
    c2 = Count("c .002",.002)
    c1 = Count("c .001",.001)
    c1f = Count("c .001f",.001)
    
    --c6 = Count("c .06",.06)
    --c5 = Count("c .05",.05)
    --c4 = Count("c .04",.04)
    --c3 = Count("c .03",.03)
    --c2 = Count("c .02",.02)
    --c1 = Count("c .01",.01)
end

function draw() 
    background(40, 40, 50)

    c1f:count()
    c1f:draw(100,50)
    c1:count()
    c1:draw(100,100)
    c2:count()
    c2:draw(100,200)
    c3:count()
    c3:draw(100,300)
    c4:count()
    c4:draw(100,400)
    c5:count()
    c5:draw(100,500)
    c6:count()
    c6:draw(100,600)
    
    text(c1f.i-c1.i,200,75)   
      
    text(c1.i-c2.i,200,150)   
    text(c2.i-c3.i,200,250)
    text(c3.i-c4.i,200,350)
    text(c4.i-c5.i,200,450)
    text(c5.i-c6.i,200,550)
    
    text(c1.i-c3.i,300,200)   
    text(c2.i-c4.i,300,300)   
    text(c3.i-c5.i,300,400)   
    text(c4.i-c6.i,300,500) 
end