Mega Code Archive

 
Categories / Delphi / Examples
 

Reading the cpu clock cycles to measure performance

With the Pentium, Intel finally introduced a benchmarking function for their CPUs. RDTSC - Read Time Stamp Counter RDTSC returns the number of clock cycles since the CPU was powered up or reset. It can be used for benchmarking without repeating your code thousands of times. Below is a quick Delphi function that will read the CPU's clock cycle count and return it as a 64bit integer. As said above, this works on a Pentium and higher, meaning 486 CPUs do not have this instruction. // Returns 64-bit count of CPU clock cycles. function GetCpuClockCycleCount: Int64; asm dw $310F // opcode for RDTSC end;