user1060028
user1060028

Reputation: 41

Why Am I Getting Different Time Values

I've tried this code in C++ on Win7x64 platform with MSVC++, and I got CPU frequency about 2900000 ticks per second.

When I run this program, my stopwatch returns about 10,000,000 tick, which means it take about 4 seconds to process my program, but my program results are ready for me in 1 second (or less) O_o.

Could you please tell me what is wrong in my code?

#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;
}

Upvotes: 2

Views: 346

Answers (2)

John Dibling
John Dibling

Reputation: 101456

Here is an example of how to use the high-frequency timer to time a block of code:

#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";
}

Upvotes: 1

Nick Bastin
Nick Bastin

Reputation: 31329

I think you have a misconception here that QueryPerformanceFrequency is telling you something about the speed of your processor - it isn't. QueryPerformanceFrequency retrieves the frequency of the high-resolution performance counter, which is not guaranteed to have any predictable relationship to your CPU clock speed. This value needs to be used in conjunction with QueryPerformanceCounter in order to get quality timing values, not with assembly that directly queries the RDTSC.

Upvotes: 2

Related Questions