Dict.CN 在线词典, 英语学习, 在线翻译

学海苦作舟,书山勤为径

留下点回忆

常用链接

统计

积分与排名

Denoise

English study

Web技术

数据压缩

一些连接

最新评论

取得CPU速度的代码

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 笨笨 阅读(1798) 评论(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
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理