Is it possible to get the exact number of CPU cycles of a code in a C program?
I tried to use the C function clock
and the assembly rdtsc
but I only had a very rough approximation, and even with loops I didn't manage to get enough accuracy.
You can find below a code that I tried (unsuccessfully). For example, to get the cycles of an incrementation, I wanted to do
clk("++foo") - clk("")
hoping to get "1".
#define __clk(x) tmp=clock() ;\
x;\
return abs(tmp-clock());
inline int clk(char* x)
{
__clk(x)
}
Do you know if there is a way to get what I want? I'm currently doing C on Debian, but if needed I also have a Windows system, and if a solution is only available in another language, that's not a problem.