testing function execution speed?

How can I test the execution speed of a function (or part of the code)?

I tried a couple ways along the lines of the snippet below (including elapsedtime and timediff() ), but I keep getting zero as a result.

startTime = os.time()
     function()
print(os.time()-startTime)

Put it in a loop that runs it 100000 times (or more, until it takes a few sec to complete)

ElapsedTime gives you fractions of a second, but only updates from one frame to the next, so put the million loop in the draw function (with a flag so that it only fires once).

os.time should be all right in setup, the problem was he only ran it once

Yeah, os.time would work just in setup, though it’s whole seconds only. I guess it depends how accurate you need to be/ how much time you mind waiting. What I find interesting, is that often the values you get vary quite a lot, even though the exact same set of instructions are being run.

@Kirl Here’s an example of how I time things. You’ll have to change the loop value depending on what you time.

function setup()
    loop=10000000
    s=os.clock()
    for z=1,loop do
        a=math.sin(z)
        a=math.cos(z)
        a=math.log(z)
        a=math.sqrt(z)
    end
    print(os.clock()-s)
end

@yojimbo2000 - I guess the iPad could be doing other things in the background, like updating apps or polling for mail.

Thanks guys!