I’m trying to find a way to get the execution time of a section of code in C. I’ve already tried both time() and clock() from time.h, but it seems that time() returns seconds and clock() seems to give me milliseconds (or centiseconds?) I would like something more precise though. Is there a way I can grab the time with at least microsecond precision?
This only needs to be able to compile on Linux.
You referred to
clock()andtime()– were you looking forgettimeofday()? That will fill in astruct timeval, which contains seconds and microseconds.Of course the actual resolution is up to the hardware.