There are different ways to measure how long code takes to execute.
If you are interested in the relative performance of certain functions, a profiler is the only way to go. Note that this will de-emphasise the impact of blocking I/O due to the computation overheads it induces.
If you want the clock-based time of certain functions, there are loads of options.
Personally I would say gettimeofday is sufficient.
If you want to get precise, use RDTSC
If you want to get really precise, you'll want something like this
t1 = rdtsc();
t2 = rdtsc();
my_code();
t3 = rdtsc();
my_code_time = (t3-t2) - (t2-t1)
You will need to repeat this block to account for thread scheduling discrepencies, and also pay attention to cacheing effects.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…