由于别人可以通过修改注册表或截获系统函数,是我无法信任我得到的结果,所以,最好能介绍一种独立的测试方法

解决方案 »

  1.   

    // Timer.h
    #pragma onceinline unsigned __int64 GetCycleCount(void)
    {
      _asm    _emit 0x0F
      _asm    _emit 0x31
    }class KTimer
    {
      unsigned __int64  m_startcycle;public:  unsigned __int64  m_overhead;  KTimer(void)
      {
        m_overhead = 0;
        Start();
        m_overhead = Stop();
      }  void Start(void)
      {
        m_startcycle = GetCycleCount();
      }  unsigned __int64 Stop(void)
      {
        return GetCycleCount()-m_startcycle-m_overhead;
      }
    };The KTimer class stores timing information in a 64-bit unsigned integer, because the 32-bit version lasts less than a second on a 200-MHz machine. The Get Cycle Count function returns a 64-bit unsigned integer representing the current CPU clock count. The result generated by the RDTSC instruction matches the C 64-bit function return value convention. So GetCycleCount is merely a single machine instruction. The Start function reads the starting clock count; Stop reads the end clock count and returns their difference. To be more accurate, we have to compensate for the time used by the RDTSC instructions and store its reading. The KTimer class constructor does this by starting and stopping the timer once to measure the amount of overhead, which can then be deducted later.Here is a sample program that uses KTimer to measure your CPU clock speed and the time it takes to create a solid brush:
    // GDISpeed.cpp
    #define STRICT
    #define WIN32_LEAN_AND_MEAN#include <windows.h>
    #include <tchar.h>#include "..\..\include\timer.h"int WINAPI WinMain(HINSTANCE hInst, HINSTANCE,
                       LPSTR lpCmd, int nShow)
    {
      KTimer timer;
      TCHAR mess[128];  timer.Start();
      Sleep(1000);
      unsigned cpuspeed10 = (unsigned)(timer.Stop()/100000);  timer.Start();
      CreateSolidBrush(RGB(0xAA, 0xAA, 0xAA));
      unsigned time = (unsigned) timer.Stop();  wsprintf(mess, _T("CPU speed       %d.%d mhz\n")
        _T("KTimer overhead %d clock cycles\n")
        _T("CreateSolidBrush %d clock cycles %d ns"),
        cpuspeed10 / 10,  cpuspeed10 % 10,
        (unsigned) timer.m_overhead,
        time, time * 10000 / cpuspeed10;  MessageBox(NULL, mess, _T("How fast is GDI?"), MB_OK);  return 0;
    }