Below is code my testing my computer's CPU frequency and timing a function using rdtsc
.
/* Code only works on x86 machine compiling with GCC */
/* Keep track of most recent reading of cycle counter */
static unsigned cyc_hi = 0;
static unsigned cyc_lo = 0;
void access_counter(unsigned *hi, unsigned *lo)
{
/* Get cycle counter */
asm("rdtsc; movl %%edx,%0; movl %%eax,%1"
: "=r" (*hi), "=r" (*lo)
: /* No input */
: "%edx", "%eax");
}
double get_counter()
{
unsigned ncyc_hi, ncyc_lo;
unsigned hi, lo, borrow;
double result;
/* Get cycle counter */
access_counter(&ncyc_hi, &ncyc_lo);
/* Do double precision subtraction */
lo = ncyc_lo - cyc_lo;
borrow = lo > ncyc_lo;
hi = ncyc_hi - cyc_hi - borrow;
return (double) hi * (1 << 30) * 4 + lo;
}
void start_counter()
{
access_counter(&cyc_hi, &cyc_lo);
}
void p()
{
sleep(1);
}
int main(int argc, char const *argv[])
{
/* Determine Clock Rate of Processor */
double MHZ;
int sleep_time = 10;
start_counter();
sleep(sleep_time);
MHZ = get_counter() / (sleep_time * 1e6);
printf("Processor Clock Rate ~= %.1f MHz\n", MHZ);
/* cat /proc/cpuinfo */
/* Time Function P */
double tsecs;
start_counter();
p();
tsecs = get_counter() / (MHZ * 1e6);
printf("%.1f seconds\n", tsecs);
return 0;
}
After running this program, it prints the processor clock rate is around 3591.8MHz, then I run cat /proc/cpuinfo
, it shows there are 8 processors (0 ~7), and some processors' cpu MHz are different, but no one is 3591.8Mhz. I am wondering how do I interpret this value, 3591.8MHz? Many thanks.