Search code examples
pythontimerfreebsdbsdgettimeofday

Python clock function on FreeBSD


While testing Pythons time.clock() function on FreeBSD I've noticed it always returns the same value, around 0.156

The time.time() function works properly but I need a something with a slightly higher resolution.

Does anyone the C function it's bound to and if there is an alternative high resolution timer?

I'm not profiling so the TimeIt module is not really appropriate here.


Solution

  • time.clock() returns the processor time. That is, how much time the current process has used on the processor. So if you have a Python script called "clock.py", that does import time;print time.clock() it will indeed print about exactly the same each time you run it, as a new process is started each time.

    Here is a python console log that might explain it to you:

    >>> import time
    >>> time.clock()
    0.11
    >>> time.clock()
    0.11
    >>> time.clock()
    0.11
    >>> for x in xrange(100000000): pass
    ... 
    >>> time.clock()
    7.7800000000000002
    >>> time.clock()
    7.7800000000000002
    >>> time.clock()
    7.7800000000000002
    

    I hope this clarifies things.