Dict.CN 在线词典, 英语学习, 在线翻译
随笔-91  评论-317  文章-0  trackbacks-0
class CCPUCalcutor
{
    unsigned __int64 m_start;
public:
    unsigned __int64 m_overhead;

    CCPUCalcutor(void)
    {
        m_overhead 
= 0;
        Start(); 
/// we get the start cycle
        m_overhead 
= Stop();
        
// then we get the stop cycle catching the overhead time
    }

    void Start(void)
    {
        m_start 
= TheCycleCount();
    }

    unsigned __int64 Stop(void)
    {
        
/// with the stop cycle we remove the overhead's time
        return TheCycleCount()-m_start-m_overhead;
    }

    inline unsigned __int64 TheCycleCount(void)
    {
        _asm _emit 0x0F
        _asm _emit 0x31

        
/// this causes a compiler warning as there is no return statement
        
/// however the _emits return a __int64 value
    }

    DWORD GetCPUSpeed()
    {
        Start();
        Sleep(
100);
        unsigned cpuspeed100 
= (unsigned)(Stop()/1000);
        return cpuspeed100
/100;
    }
};
client只要这样调用就可以得到CPU的速度了:
CCPUCalcutor cpuCalc;
cpuCalc.GetCPUSpeed();
posted on 2007-05-15 16:02 笨笨 阅读(1020) 评论(2)  编辑 收藏 引用 所属分类: Windows编程

评论:
# re: 取得CPU速度的代码 2007-05-15 22:34 | To Be C++
只能大概精确 Sleep具体延时的时间是个未知数  回复  更多评论
  
# re: 取得CPU速度的代码 2007-05-25 16:18 | en
__asm rdtsc 指令,vc7.0以后可以直接写,不用_emit
  回复  更多评论
  

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]