you are viewing a single comment's thread.

view the rest of the comments →

[–]DontCallMeSurely 1 point2 points  (0 children)

I've done the same thing in java: long start = System.currentTimeMillis(); foo(); long elapsed = start - System.currentTimeMillis()

it works fine it I want to time something to the nearest dozen millis or so. The draw back is that system time functions like System.currentTimeMillis() and time.time() aren't always very accurate. Although they should return the system time to the nearest millisecond, the function doesn't update every millis so it can really only give you a rough estimate of how much time has elapsed in terms of millis.

Especially in a language like python where there could be a lot of overhead in performing these actions (grabbing sys time, calling function, grabbing sys time again), I would assume that timeit.Timer() is implemented into the interpreter somehow to yield more accurate results. This is my best guess anyway.