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.
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:
I hope this clarifies things.
time.clock() is implemented to return a double value resulting from
Why do you think time.time() has bad resolution? It uses gettimeofday, which in turn reads the hardware clock, which has very good resolution.
Python's time.clock calls C function clock(3) --
man clock
should confirm that it's supposed to work on BSD, so I don't know why it's not working for you. Maybe you can try working around this apparent bug in your Python port by usingctypes
to call the clock function from the system C library directly (if you have said library as a .so/.dynlib/.dll or whatever dynamic shared libraries are called on FreeBSD)?time.time is supposed to be very high resolution, BTW, as internally it calls gettimeofday (well, in a properly built Python, anyway) -- what resolution do you observe for it on your system?
Edit: here's
wat.c
, a BSD-specific extension (tested on my Mac only -- sorry but I have no other BSD flavor at hand right know) to work around this apparent FreeBSD port problem:And here's the
setup.py
to put in the same directory:The URL is correct, so you can also get these two files zipped up here.
To build & install this extension,
python setup.py install
(if you have permission to write in your Python's installation) orpython setup.py build_ext -i
to write wat.so in the very directory in which you put the sources (and then manually move it wherever you prefer to have it, but first try it out e.g. withpython -c'import wat; print repr(wat.time())'
in the same directory in which you've built it).Please let me know how it works on FreeBSD (or any other Unix flavor with
gettimeofday
!-) -- if the C compiler complains aboutgettimeofday
, you may be on a system which doesn't want to see its second argument, try without it!-).time.clock()
returns CPU time on UNIX systems, and wallclock time since program start on Windows. This is a very unfortunate insymmetry, in my opinion.You can find the definition for
time.time()
in the Python sources here (link to Google Code Search). It seems to use the highest-resolution timer available, which according to a quick Googling isgettimeofday()
on FreeBSD as well, and that should be in the microsecond accuracy class.However, if you really need more accuracy, you could look into writing your own C module for really high-resolution timing (something that might just return the current microsecond count, maybe!). Pyrex makes Python extension writing very effortless, and SWIG is the other common choice. (Though really, if you want to shave as many microseconds off your timer accuracy, just write it as a pure C Python extension yourself.) Ctypes is also an option, but probably rather slow.
Best of luck!