os.difftime() not calculating difference between two times correctly?

So, I created a small program in Codea which displays the number of times you have tapped the screen. I wanted the “timer” (os.clock()) to start when the user first taps the screen, and I wanted it to end when they’ve reached 5 taps. I then tried to use os.difftime() to calculate the difference in seconds between the two times. The thing is, the number of seconds between the first and fifth tap is not correct; it’s slow. If anyone could help me with this issue, that would be fantastic. Here’s the code (please note I’m not too experienced with Codea or Lua):

x = 0
p = 0
k = 0
n = 0
function setup()
    supportedOrientations(PORTRAIT)
    str = ""
    bcolor= fffff
    l=100
    r=300
    b=575
    t=625
end

function draw()
    background(0, 0, 0, 255)
    stroke(255, 255, 255, 255)
    strokeWidth(10)
    fill(0, 103, 255, 255)
    ellipseMode(CENTER)
    ellipse(CurrentTouch.x, CurrentTouch.y, 200)
    textMode(CENTER)
    rectMode(CENTER)
    font("Futura-Medium")
    fontSize(23)
    fill(255, 255, 255, 255)
    if x == 0 then
        text("Tap here to start!",350,700)
    elseif x == 1 then
        text("You've tapped "..x.." time!",350,700)
    else
        text("You've tapped "..x.." times!",350,700)
    end
    if x == 5 then
        p = p + 1
        if p == 1 then
            print("Congratulations, you've reached 5 taps in "..os.difftime(os.clock(),begintime) .." seconds!")
        end
    end
    if x == 200 then
        k = k + 2
        if k == 2 then
            print("Congratulations, you've reached 200 taps in ".. os.difftime(os.clock(),begintime) .." seconds. Fantastic!")
        end
    end
end

function touched(touch)
    if touch.state == BEGAN then
        n = n + 1
        if n == 1 then
            begintime = os.clock()
        end
    end
    if touch.state == ENDED then
        x = x + 1
    end
end

hello @mcCann try to google things, most answers are there: if i google ‘lua difftime’ the first result gives the answer: difftime is to be used with os.time, not os clock!

@McCann You can use os.clock() to get what you want. Here’s an example to show you.


function setup()
    st=os.clock()    -- start
    for z=1,1000000 do    -- waste some time
        a=math.sqrt(z)
    end
    en=os.clock()    -- end
    print(st)    -- print start
    print(en)    -- print end
    print(en-st)    -- print difference
end

@Dave1707 note that os.clock is not the real time, but the total cpu time used by the program, which is, i think, not wht he wants. Maybe elapsedTime would be better?

Thanks @Jmv38, I had googled things about this but I just didn’t google the right things.

Also, thanks @dave1707, that makes a lot more sense: however the difference between the two times is still not accurate, in fact it says 2.5 seconds when it really was 10. I wonder what the problem could be?

The only thing I changed was this:

print("Congratulations, you've reached 5 taps in ".. os.clock() - begintime .." seconds!")

@McCann See the response from @Jmv38. Apparently os.clock only gives an accurate time if the CPU is doing extensive work, like if you’re timing a tight loop.

@Jmv38 Ahh, thank you so much, works perfectly now. :slight_smile: