IA32周期计数器

IA32处理器包含一个运行在时针周期级的计时器,这个计时器是一个特殊的寄存器,每个时钟周期它都会加1。IA32并提供特殊的指令读取该计时器,利用该计时器,可以很容易的计算出CPU的时钟周期(频率)。

 程序如下:

#include  < windows.h >
// #include <stdlib.h>
#include  < stdio.h >

static  unsigned cyc_hi  =   0 ;
static  unsigned cyc_lo  =   0 ;
/* 寄存器使用惯例:
**eax,ecx,edx由调用者保存,ebx,esi,edi由被调用者保存
*/
// vc下的嵌入汇编
void  access_counter(unsigned  * hi, unsigned  * lo)
{
    _asm{
        rdtsc
        mov ecx,hi
        mov dword ptr[ecx],edx
        mov ecx,lo
        mov dword ptr[ecx],eax
    }
}
// gcc的嵌入汇编
//  void access_counter(unsigned *hi, unsigned *lo)
//  {
//      asm("rdtsc; movl %%edx,%0; movl %%eax,%1"   /* Read cycle counter */
//          : "=r" (*hi), "=r" (*lo)                /* and move results to */
//          : /* No input */                        /* the two outputs */
//      : "%edx", "%eax");
//  }

/*  Record the current value of the cycle counter.  */
void  start_counter()
{
    access_counter(
& cyc_hi,  & cyc_lo);
}

/*  Return the number of cycles since the last call to start_counter.  */
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 
=  cyc_lo  >  ncyc_lo;
    hi 
=  ncyc_hi  -  cyc_hi  -  borrow;
    result 
=  ( double ) hi  *  ( 1   <<   30 *   4   +  lo;
    
return  result;
}

/*  $begin mhz  */
/*  Estimate the clock rate by measuring the cycles that elapse  */  
/*  while sleeping for sleeptime seconds  */
double  mhz( int  verbose,  int  sleeptime)
{
    
double  rate;
    
    start_counter();
    
// c库的sleep函数以秒为单位
    
// sleep(sleeptime);
    
// win32 API的Sleep以毫秒为单位
    Sleep(sleeptime);
    
// linux
    
// rate = get_counter() / (1e6*sleeptime);

    
// windows
    rate  =  get_counter()  /  (1e3 * sleeptime);

    
if  (verbose) 
        printf(
" Processor clock rate ~= %.1f MHz\n " , rate);
    
return  rate;
}
int  main()
{
    
int  i;
    
for  (i = 0 ;i < 10 ;i ++ )
    {
        
// 线程睡眠1秒
        
// mhz(1,1);   // linux下
        
// windows下
        mhz( 1 , 1000 ); 
    }
    
return   0 ;
}

 windows下输出:

 

Linux下输出:

 

本机的处理器为:Pentium(R) 4 CPU 1.60GHz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值