System .nanoTime () equivalent in C

Possible duplicate:
C ++ timer function to ensure time in nano seconds

I need to exit the loop when approaching 3 seconds, so I need to calculate the elapsed time.

I am moving code from Java to C, and I used simple System.nanoTime () in Java,

How can I do this in C?

I noticed that time (NULL) will return seconds, but I'm looking for more accuracy.

Thank you in advance

+3
source share
2 answers

use gettimeofday , it has a resolution in microseconds. System.nanoTime () in Java is usually implemented using gettimeofday on * nixes

0
source

clock() C :

#include <time.h>
#define RUNTIME_MAX_SEC 3

clock_t start = clock();
while(clock() - start < CLOCKS_PER_SEC * RUNTIME_MAX_SEC)
{ ... }
0

Source: https://habr.com/ru/post/1733510/


All Articles