Is there some function in c under linux that one can use to count clock cycles/nr of ticks? or similar? preferably in nano secs.
Is there some function in c under linux that one can use to count clock cycles/nr of ticks? or similar? preferably in nano secs.
You'll never have nanoseconds precision, well not with the computer we are using these days.
I couldn't say if there's such function on Linux. I bet there is; someone else might answer you.
But if you can write assembly for IA-32 processor (or Intel 64 architecture processor), then you should look at the RDTSC (ReaD Time Stamp Counter) instruction.
somthing like
Code:inline unsigned long rdtsc() { #if defined(__ia64) /* for Intel Itanium 64 bit processor */ unsigned long x; __asm__ __volatile__("mov %0=ar.itc" : "=r"(x) :: "memory"); while (__builtin_expect ((int) x == -1, 0)) __asm__ __volatile__("mov %0=ar.itc" : "=r"(x) :: "memory"); return x; #else /* for all others */ unsigned long long x; unsigned int low,high; __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high)); x = high; x = (x<<32)+low; return x; #endif }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler