开发者

light-weight timer in C

开发者 https://www.devze.com 2023-02-21 18:19 出处:网络
I\'m looking for a light weight timer to measure timing of few sections of a C code. This t开发者_StackOverflowimer implementation shouldn\'t add to the overall program execution time.Look at clock_ge

I'm looking for a light weight timer to measure timing of few sections of a C code. This t开发者_StackOverflowimer implementation shouldn't add to the overall program execution time.


Look at clock_gettime for POSIX-compliant platforms; you can do it yourself really easily by comparing one timestamp with one generated a little later.

Remember to use the CLOCK_PROCESS_CPUTIME_ID or CLOCK_THREAD_CPUTIME_ID parameters to specify that you want CPU time taken just by that process (and its children) or thread, and not the wider, absolute, "wall" time.

An alternative on Windows might be GetProcessTimes.


What is time?

Imagine your code took 3 miliseconds ... but it runs on 3 cores ... it used 2 milliseconds on core 1; 1.5 milliseconds on core 2; and 1.2 milliseconds on core 3 for a total of 4.7 milliseconds.

So ... is 3 milliseconds the same as 4.7 milliseconds?

Oh, don't forget that those 4.7 milliseconds were in fact also used to filter incoming internet connections and to download anti-virus database.

Use a profiler, and even then, don't trust the results :)


For POSIX, try gettimeofday() (obsolescent) clock_gettime().
For Windows, apparently, you can use GetSystemTime().


The closest thing to profiling without observation interference is oprofile. But it can't directly measure intervals; it only gives you a statistical map of where the whole program (or whole system) is spending its time.

If you really want cheap interval timing, on x86 you can use the rdtsc instruction in inline asm.

static inline unsigned rdtsc()
{
    unsigned x;
    __asm__ __volatile__ ( "rdtsc" : "=a"(x) : : "edx" );
    return x;
}

Use this to save the timestamp before and after and take the difference. You could modify this code to save the full 64-bit result, but I opted just for the 32-bit result assuming you'll be timing intervals shorter than 4 billion cycles and don't want to waste time on 64-bit subtraction.

0

精彩评论

暂无评论...
验证码 换一张
取 消