Measuring execution time in nanoscale precision with a C class function requires a timer that offers higher accuracy than the default clock_t implementation.
The provided code utilizes the clock_t type to measure time in seconds. For nanoscale precision, you need an alternative timer implementation.
Linux (and BSD)
Use clock_gettime():
#include <sys/time.h> int main() { timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return 0; }
Windows
Use QueryPerformanceCounter:
#include <windows.h> int main() { LARGE_INTEGER freq, pc; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&pc); return 0; }
Additional Considerations:
References:
The above is the detailed content of How Can I Achieve Nanoscale Time Measurement in C ?. For more information, please follow other related articles on the PHP Chinese website!