Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

rdtsc()

Christina_C_Intel
1,024 Views
Does anyone have any examples or insight on how to print the rdtsc() value on IA-32? I'm using intrinsics, but asmlanguage will be helpful too.
Thanks
0 Kudos
7 Replies
Intel_C_Intel
Employee
1,024 Views
How about this?
unsigned __int64 tick = 0;
void getTick(void)
{
__asm{
_emit 0x0f
_emit 0x31
lea ecx,tick
mov dword ptr [ecx], eax
mov dword ptr [ecx+4],edx
}
}
main() {
while (1) {
getTick();
printf("%I64u ", tick);
}
}
Aart Bik
0 Kudos
Christina_C_Intel
1,024 Views
Yes, that works.
Thanks
0 Kudos
simbalee
Beginner
1,024 Views
Does it work on a computer with multi-core?
It seems not always work on Core Duo.
0 Kudos
TimP
Honored Contributor III
1,024 Views

If you want the same code to work in both 32- and 64-bit compilation, you should use the _rdtsc() intrinsic, supported by Intel and MS C++.

rdtsc works fine on Core 2 Duo, where it is based on the Front Side Bus clock. I do find that I must repeat a code over a longer time intervalto get repeatable timing on Core 2 than on earlier Intel architectures. My results are sufficiently repeatable for elapsed time intervals of 0.0001 seconds or more.

0 Kudos
simbalee
Beginner
1,024 Views

I am using a 32bit WinXP Prof. on a Core Duo E6400, instead of 64bit OS. The code I am using to get clocktick is something like this:

unsigned __int64 GetCycleCount()
{
__asm_emit 0x0F
__asm_emit0x31
}

It does work on the PC, but not always work. And it seems has nothing to do with elapsed time. Say, it can measure a thread that run for less than 0.1ms, on the other hand, it gives 0 to a thread that may last more than 100ms. And the result is repeatable, which means the function always gives zero for some conditions. That is very weird.

0 Kudos
TimP
Honored Contributor III
1,024 Views

It looks like you are seeing only the low order 32 bits of the timer registers. Aart's example shows you how to get all 64 bits in 32-bit asm. Compiling in 64-bit mode (should you change OS in the future), you need a different version to get 64 bits. With gcc compatible compilers, you need yet other variations. So, I repeat the suggestion of looking up __rdtsc() http://msdn2.microsoft.com/en-us/library/twchhe95(VS.80).aspx

if you are willing to use a compiler recent enough to support it. I think even the free Microsoft compilers from 2003 on include this intrinsic.

0 Kudos
simbalee
Beginner
1,024 Views
You are right. Dr. Bik's code works well on Core Duo. I got the code some years before and did not realized that it was for IA-32.
0 Kudos
Reply