2011-11-22 67 views
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; 
} 
+1

不能被編譯的x64。 x64不支持'asm'。 –

+0

你應該使用'__rdtsc()'內部的windows x64 – Necrolis

+0

謝謝你的建議。當我使用QueryPerformanceCounter()時,QueryPerformanceCounter和__rdtsc() – user1060028

回答

2

我覺得你這裏QueryPerformanceFrequency告訴你關於你的處理器的速度東西誤解 - 它不是。 QueryPerformanceFrequency檢索高分辨率性能計數器的頻率,該計數器不保證與CPU時鐘速度有任何可預測的關係。此值需要與QueryPerformanceCounter一起使用,以獲得高質量的定時值,而不是直接查詢RDTSC的彙編。

+0

有什麼區別,有時它會返回0.是否是錯誤結果? – user1060028

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"; 
} 
+0

謝謝你的代碼。但爲什麼'freq'= 2.9m。我使用3.06GHz的CPU – user1060028

相關問題