The time_t structure is likely to be an integer, which means that it will have a second resolution.
The first piece of code: it will only count the time when the processor did something, so when you sleep (), it wonβt count anything. You can get around it by calculating your sleep time (), but after a while it will probably start to drift.
The second part: only the resolution of seconds, not so large if you need a subsecond time indication.
To show the time with the best resolution, you can get something like this:
double getUnixTime(void) { struct timespec tv; if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0; return (tv.tv_sec + (tv.tv_nsec / 1000000000.0)); } double start_time = getUnixTime(); double stop_time, difference; doYourStuff(); stop_time = getUnixTime(); difference = stop_time - start_time;
In most systems, the resolution will be up to several microseconds, but it can vary depending on different processors and, possibly, even on the main kernel versions.
Plecharts Sep 01 2018-12-12T00: 00Z
source share