Python Clock Function on FreeBSD

When checking the Pythons time.clock () function on FreeBSD, I noticed that it always returns the same value, around 0.156

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

Anyone involved with the C function, and is there an alternative high-resolution timer?

I do not profile, so the TimeIt module is not suitable here.

0
python bsd freebsd timer gettimeofday
Jul 10 '09 at 15:02
source share
4 answers

time.clock () returns the processor time. That is, how much time the current processor used on the processor. Therefore, if you have a Python script called "clock.py" that does import time;print time.clock() , it will print exactly the same every time you start it, since every process starts every time.

Here is the python console log that can explain 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 

Hope this clarifies the situation.

+1
Jul 10 '09 at 15:37
source share

Python time.clock calls C function clock (3) - man clock should confirm that it should work on BSD, so I don't know why it does not work for you. Perhaps you can try to get around this apparent error in your Python port using ctypes to directly call the clock function from the System C library (if you specified the library as .so / .dynlib / .dll or any dynamic shared libraries are called on FreeBSD)?

time.time should be very high resolution, BTW, since inside it calls gettimeofday (well, in correctly built Python, anyway) - what resolution do you observe for it on your system?

Edit : here is wat.c , a special BSD extension (tested only on my Mac - sorry, but I don't have another BSD flavor that knows well) to get around this obvious FreeBSD port problem:

 #include "Python.h" #include <sys/time.h> static PyObject * wat_time(PyObject *self, PyObject *args) { struct timeval t; if (gettimeofday(&t, (struct timezone *)NULL) == 0) { double result = (double)t.tv_sec + t.tv_usec*0.000001; return PyFloat_FromDouble(result); } return PyErr_SetFromErrno(PyExc_OSError); } static PyMethodDef wat_methods[] = { {"time", wat_time, METH_VARARGS, PyDoc_STR("time() -> microseconds since epoch")}, {NULL, NULL} /* sentinel */ }; PyDoc_STRVAR(wat_module_doc, "Workaround for time.time issues on FreeBsd."); PyMODINIT_FUNC initwat(void) { Py_InitModule3("wat", wat_methods, wat_module_doc); } 

And here setup.py put in the same directory:

 from distutils.core import setup, Extension setup (name = "wat", version = "0.1", maintainer = "Alex Martelli", maintainer_email = "aleaxit@gmail.com", url = "http://www.aleax.it/wat.zip", description = "WorkAround for Time in FreeBSD", ext_modules = [Extension('wat', sources=['wat.c'])], ) 

The URL is correct, so you can also get these two files encrypted here .

To create and install this extension, python setup.py install (if you have write permission in your Python installation) or python setup.py build_ext -i to write wat.so in the very directory where you put the sources (and then manually move it wherever you want, but first try, for example, using python -c'import wat; print repr(wat.time())' in the same directory in which you created it).

Please let me know how this works on FreeBSD (or any other Unix style with gettimeofday ! -) - if the C compiler complains about gettimeofday , you may be on a system that does not want to see its second argument, try without it! -).

+3
Jul 10 '09 at 15:11
source share

time.clock() returns the CPU time on UNIX systems and the operating time with Windows since starting the program. In my opinion, this is a very unsuccessful asymmetry.

You can find the definition for time.time() in Python sources here (link to Google Code Search). It seems that the timer with the highest resolution is used, which according to fast gettimeofday() is gettimeofday() on FreeBSD, and this should be in the microsecond accuracy class.

However, if you really need higher accuracy, you can think about creating your own C-module for a very high resolution (something that can simply return the current microsecond count, maybe!). Pyrex makes writing the Python extension very easy, and SWIG is another common choice. (Although in fact, if you want to shave so many microseconds off your timer accuracy, just write it as the Python C extension itself.) Ctypes is also an option, but probably pretty slow.

Good luck

+1
Jul 10 '09 at 15:11
source share

time.clock () is implemented to return a double value obtained from

  ((double)clock()) / CLOCKS_PER_SEC 

Why do you think time.time () has poor resolution? It uses gettimeofday, which in turn reads a hardware clock that has a very good resolution.

0
Jul 10 '09 at 15:12
source share



All Articles