But I need the precise timing in nanoseconds.
Then use
int clock_gettime(clockid_t clk_id, struct timespec *tp);
It returns time in seconds and nanoseconds
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
Useful links: