社区应用 最新帖子 精华区 社区服务 会员列表 统计排行 社区论坛任务 迷你宠物
  • 2352阅读
  • 0回复

编程实现测试CPU的速度

级别: 终身会员
发帖
3743
铜板
8
人品值
493
贡献值
9
交易币
0
好评度
3746
信誉值
0
金币
0
所在楼道
CPU的速度随温度和电压的变化而变化,如何随时查看CPU的速度?下面我们通过编程实现。在这个过程中,要用到汇编语言的知识。 Ie_I7YJ  
YMB~[]$V<  
  第一步:生成一个基于对话框的工程CPUSpeed。其他选项我们可以都取其默认值。 78Y@OL_$  
9YKDguG  
  第二步:在对话框上添加一个按钮,名称为"测试CPU速度",双击此按钮生成此按钮的处理函数,OnButton1。 JeQ[qQ  
Lo`F  
  第三步:在CPUSpeedDlg.cpp文件中定义类Ctime,在OnButton1中添加处理代码,最后文件CPUSpeedDlg.cpp变成如下: L(o#4YH}>J  
  1. // CPUSpeedDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CPUSpeed.h"
  5. #include "CPUSpeedDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. ///////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. inline unsigned __int64 theCycleCount(void)
  14. {
  15.  _asm _emit 0x0F
  16.  _asm _emit 0x31
  17.  /// this causes a compiler warning as there is no return statement
  18.  /// however the _emits return a __int64 value
  19. }
  20. class CTimer
  21. {
  22.  unsigned __int64 m_start;
  23.  public:
  24.   unsigned __int64 m_overhead;
  25.  CTimer(void)
  26.  {
  27.   m_overhead = 0;
  28.   Start(); /// we get the start cycle
  29.   m_overhead = Stop();
  30.   // then we get the stop cycle catching the overhead time
  31.  }
  32.  void Start(void)
  33.  {
  34.   m_start = theCycleCount();
  35.  }
  36.  unsigned __int64 Stop(void)
  37.  {
  38.   /// with the stop cycle we remove the overhead's time
  39.   return theCycleCount()-m_start-m_overhead;
  40.  }
  41. };
  42. void CCPUSpeedDlg::OnButton1()
  43. {
  44.  // TODO: Add your control notification handler code here
  45.  CString strRawClockFrequency;
  46.  
  47.  CTimer timer;
  48.  long tickslong;
  49.  long tickslongextra;
  50.  timer.Start();
  51.  Sleep(1000);
  52.  unsigned cpuspeed100 = (unsigned)(timer.Stop()/10000);
  53.  tickslong = cpuspeed100/100;
  54.  tickslongextra = cpuspeed100-(tickslong*100);
  55.  strRawClockFrequency.Format("%d.%d MHZ estimate ", tickslong,tickslongextra );
  56.  AfxMessageBox("CPU速度为"+strRawClockFrequency);
  57.  }
  58.  class CAboutDlg : public CDialog
  59.  {
  60.  ……以下为编程环境生成时自动生成的代码。
wh6&>m#r  
好了,现在点击按钮"测试CPU速度"就可以弹出对话框告诉我们CPU的速度了。
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
描述
快速回复

您目前还是游客,请 登录注册
如果您在写长篇帖子又不马上发表,建议存为草稿
认证码:
验证问题:
3+5=?,请输入中文答案:八 正确答案:八