5

I have read this question, but I would like to get better timing precision than a second: is this possible with some function of libc?

This is to use inside with nogil, so of course no Python is allowed...

Community
  • 1
  • 1
P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
  • In which part of your cython? `.pyx` or `.c[pp]`? For c: http://stackoverflow.com/questions/5833094/get-a-timestamp-in-c-in-microseconds. There are many ways to do it in c++. Then maybe you can make a wrapper in .pyx. – greedy52 Feb 17 '17 at 18:04
  • @XinHuang in .pyx, I don't even have a .c (except for the one generated by cython) – P. Camilleri Feb 17 '17 at 19:38

2 Answers2

7

You can use POSIX clock_gettime():

from posix.time cimport clock_gettime, timespec, CLOCK_REALTIME

cdef timespec ts
cdef double current
clock_gettime(CLOCK_REALTIME, &ts)
current = ts.tv_sec + (ts.tv_nsec / 1000000000.)

This snippet store in current the number of seconds since epoch with up to a nanosecond precision (depending on your system).

You can also use gettimeofday(), but it is now deprecated:

POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead.

DurandA
  • 1,095
  • 1
  • 17
  • 35
-5

Why dont you use datetime? For instance datetime.datetime.now() will give you current time value including milliseconds.

Rishabh
  • 105
  • 2
  • 11