I want to time a function call with rdtsc. So I measured it in two ways as follows.
Have you tried clock_gettime(CLOCK_MONOTONIC, &tp)? Should be quite near to reading the cycle counter by hand, also keep in mind that the cycle counter may not be synchronized between cpu cores.
clock_gettime(CLOCK_MONOTONIC, &tp)