Programming Tips & Tricks

Home Categories: C# | C++ | General | Other

C++ Tips & Tricks: CPUID and RDTSC

Most modern compilers and standard libraries should support CPUID and RDTSC (typically via functions named __cpuid and __rdtsc), but if not, you can use the emit function to directly insert the respective opcodes into an executable:
#define rdtsc __asm __emit 0fh __asm __emit 031h
#define cpuid __asm __emit 0fh __asm __emit 0a2h
cpuid can be used to get information on the processor, rdtsc (Real Time Stamp Counter) will return the current cycle count of the cpu which can be used for time measuring.

Related Tips: Cycle counting with the RDTSC instruction