Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.nanoTime() equivalent in C [duplicate]

Tags:

java

c

time

Possible Duplicate:
C++ Timer function to provide time in nano seconds

I need to get out of a loop when approaching 3 seconds, so I need to calculate the elapsed time.

I'm moving some code from Java to C, and I was using the easy System.nanoTime() in Java,

How would I do that in C?

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

Thank you in advance

like image 982
David Robles Avatar asked Mar 28 '26 19:03

David Robles


1 Answers

For the resolution you want, clock() from the C standard library is sufficient:

#include <time.h>
#define RUNTIME_MAX_SEC 3

clock_t start = clock();
while(clock() - start < CLOCKS_PER_SEC * RUNTIME_MAX_SEC)
{ ... }
like image 175
Christoph Avatar answered Apr 01 '26 07:04

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!