2

我已经在带有 MSVC++ 的 Win7x64 平台上的 C++ 中尝试了这段代码,我得到的 CPU 频率约为每秒 2900000 个滴答声。

当我运行这个程序时,我的秒表返回了大约 10,000,000 个滴答,这意味着处理我的程序需要大约 4 秒,但我的程序结果在 1 秒(或更短)O_o 内为我准备好了。

你能告诉我我的代码有什么问题吗?

#include <iostream>
#include "header.h"
#include <fstream>
#include <string>
#include <sstream>
#include <strsafe.h>
#include <direct.h>
#include <string.h>



using namespace std;

#define CV_TO_NANO 1000000000
#define CV_TO_MICRO 1000000
#define CV_TO_MILLI 1000

 unsigned __int64 inline GetRDTSC()
{
   __asm
   {
      ; Flush the pipeline
      XOR eax, eax
      CPUID
      ; Get RDTSC counter in edx:eax
      RDTSC
   }
}

unsigned __int64 RunTest(TCHAR *AppName, TCHAR *CmdLine);

 void main()
 {  
     unsigned __int64 start = 0;
     unsigned __int64 stop = 0;
     unsigned __int64 freq = 0;
     float rps;
     ofstream dataFile;


     // get processor freq
     QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
     cout <<"freq (count per second): "<< freq << endl;
     // round per second
     rps = 1.0/(freq);
     cout <<"rps (1/rps): "<< rps << endl;
     dataFile.open ("d:/dataC.txt",ios::out );
     for(int i = 0;i<200;i++)
     {
        SetProcessAffinityMask(GetCurrentProcess(),0x0001);
        SetThreadAffinityMask(GetCurrentThread(),0x0001);
        cout << RunTest(L"D:\\Child\\Child.exe", NULL);
     }
    getchar();
    return;
 }

unsigned __int64 RunTest(TCHAR *AppName, TCHAR *CmdLine)
{
    unsigned __int64 start = 0;
    unsigned __int64 stop = 0;
    PROCESS_INFORMATION processInformation;
    STARTUPINFO startupInfo;
    memset(&processInformation, 0, sizeof(processInformation));
    memset(&startupInfo, 0, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);

    BOOL result;
    start = GetRDTSC();
    result = ::CreateProcess(AppName, CmdLine, NULL, NULL, FALSE, REALTIME_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation);
    stop = GetRDTSC();
    getchar();
    if (result == 0)
    {
        wprintf(L"ERROR: CreateProcess failed!");
    }
    else
    {
        WaitForSingleObject( processInformation.hProcess, 0 );
        CloseHandle( processInformation.hProcess );
        CloseHandle( processInformation.hThread );
    }
    return stop - start;
}
4

2 回答 2

2

我认为您在这里有一个误解,QueryPerformanceFrequency即告诉您有关处理器速度的一些信息-事实并非如此。 QueryPerformanceFrequency检索高分辨率性能计数器的频率,不保证与您的 CPU 时钟速度有任何可预测的关系。QueryPerformanceCounter为了获得高质量的时序值,需要将此值与直接查询 RDTSC 的程序集结合使用。

于 2011-11-22T15:19:58.093 回答
1

下面是一个如何使用高频定时器对代码块进行计时的示例:

#include <Windows.h>
#include <iostream>
using namespace std;

int main()
{
    LARGE_INTEGER li = {};
    __int64 freq, start, stop;

    QueryPerformanceFrequency(&li);
    freq = li.QuadPart;

    cout << "Counter Frequency: " << freq << "\n";

    QueryPerformanceCounter(&li);
    start = li.QuadPart;

    for( int i = 0; i < 1000000; ++i )
    {
        int n = i * rand();
    } 

    QueryPerformanceCounter(&li);
    stop = li.QuadPart;

    double elapsed_seconds = static_cast<double>(stop-start) / static_cast<double>(freq);

    cout << "Elapsed Time: " << elapsed_seconds << " seconds\n";
}
于 2011-11-22T15:29:33.367 回答