2011-11-22 60 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

相关问题