MSDN里提到
QueryPerformanceFrequency FunctionThe QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running.Syntax    BOOL QueryPerformanceFrequency(      
        LARGE_INTEGER *lpFrequency
    );Parameters    lpFrequency
        [out] Pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware does not support a high-resolution performance counter, this parameter can be zero. Return Value    If the installed hardware supports a high-resolution performance counter, the return value is nonzero.    If the function fails, the return value is zero. To get extended error information, call GetLastError. For example, if the installed hardware does not support a high-resolution performance counter, the function fails.Function Information    Minimum DLL Version kernel32.dll
    Header Declared in Winbase.h, include Windows.h
    Import library Kernel32.lib
    Minimum operating systems Windows 95, Windows NT 3.1
    Unicode Implemented as Unicode version.See Also    Timers Overview, QueryPerformanceCounter
=======================华丽的分隔线===================================
以下都是在32位Windows XP上,使用mingw32,gcc4.3(tdm版)的结果在一台1.8G的Core 2 Duo上,以下代码返回的值比较奇怪:
__int64 persecond;
QueryPerformanceFrequency((LARGE_INTEGER*) &persecond);
............
cout << "  performance frequency is " << persecond << " Hz" << endl;
最后输出是3579545 Hz,比真实值少了2000倍。由于是双核,1.8x2=3.6比较接近3.58 MHz的有效数字3.58。但是差一千倍是什么原因呢?另外,我在Pentium D 2.8G(EMT64,双核)上测的结果却仍是2.8G(具体输出大概是2700000000),没有这种问题。谁有Itanium的机器,能看看Itanium上的结果如何吗?

解决方案 »

  1.   

    cout  < < "  performance frequency is "  < < persecond  < < " Hz"  < < endl; 这行能编译通过吗?我在VC6下面编译不能通过
      

  2.   

    #include <iostream>
    #include <windows.h>
    #include <climits>using namespace std;int maxSubSum1(int a[], const unsigned int len)
    {
    int maxSum = INT_MIN;
    unsigned int i, j, k;
    for(i=0; i<len; i++)
    {
    for(j=i; j<len; j++)
    {
    int sum = 0;
    for(k=i; k<=j; k++) sum += a[k];
    if (sum > maxSum) maxSum = sum;
    }
    }
    return maxSum;
    }int maxSubSum2(int a[], const unsigned int len)
    {
    int maxSum = INT_MIN;
    unsigned int i, j;
    for(i=0; i<len; i++)
    {
    int sum = 0;
    for(j=i; j<len; j++)
    {
    sum += a[j];
    if (sum > maxSum) maxSum = sum;
    }
    }
    return maxSum;
    }int max3(int a, int b, int c)
    {
    int max = (a>b) ? a : b;
    return ((max>c) ? max : c);
    }int maxSumRec(int a[], int left, int right)
    {
    if(left == right)  // Base case
    if(a[left]>INT_MIN) return a[left];
    else return INT_MIN; int center = (left+right)/2;
    int maxLeftSum  = maxSumRec(a, left, center);
    int maxRightSum = maxSumRec(a, center+1, right); int maxLeftBorderSum = INT_MIN, leftBorderSum = 0, i;
    for(i=center; i>=left; i--)
    {
    leftBorderSum += a[i];
    if(leftBorderSum > maxLeftBorderSum)
    maxLeftBorderSum = leftBorderSum;
    }
    int maxRightBorderSum = INT_MIN, rightBorderSum = 0;
    for(i=center+1; i<=right; i++)
    {
    rightBorderSum += a[i];
    if(rightBorderSum>maxRightBorderSum)
    maxRightBorderSum = rightBorderSum;
    }
    return max3(maxLeftSum, maxRightSum, maxLeftBorderSum+maxRightBorderSum);
    }int maxSubSum3(int a[], const unsigned int len)
    {
    return maxSumRec(a, 0, len-1);
    }int maxSubSum4(int a[], const unsigned int len)
    {
    int maxSum = INT_MIN, thisSum = 0;
    unsigned int j; for(j=0; j<len; j++)
    {
    thisSum += a[j];
    if(thisSum>maxSum) maxSum = thisSum;
    if(thisSum<0) thisSum = 0;
    }
    return maxSum;
    }void test(int f(int [], const unsigned int), int a[], const unsigned int len)
    {
    int sum = INT_MIN;
    unsigned int i;
    __int64 t1, t2;
    for (i=3; i<=len; i++)
    {
    QueryPerformanceCounter((LARGE_INTEGER *)&t1);
    sum = f(a, i);
    QueryPerformanceCounter((LARGE_INTEGER *)&t2);
    __int64 duration = t2-t1;
    cout /*<< "Size: " << i 
    << ", Maximun sum is " << sum 
    << ", Time: "*/ << duration /*<< " counts." */<< endl;
    }
    }int main()
    {
    int a[] = {-1, 10, -3, 12, -4, -3, 23, -45, 34, 1, -5, 99, -3, -85, 30};
    const unsigned int len = sizeof(a)/sizeof(int);
    __int64 persecond;
    QueryPerformanceFrequency((LARGE_INTEGER*) &persecond);
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    cout << "Hardware information: " << endl;
    cout << "  OEM ID: " << si.dwOemId;
    cout << "  Number of processors: " << si.dwNumberOfProcessors << endl; 
    cout << "  Page size: " << si.dwPageSize << endl;
    cout << "  Processor type: " << si.dwProcessorType << endl;
    cout << "  Minimum application address: " << si.lpMinimumApplicationAddress << endl; 
    cout << "  Maximum application address: " << si.lpMaximumApplicationAddress << endl; 
    cout << "  Active processor mask: " << si.dwActiveProcessorMask << endl; 
    cout << "  CPU frequency is " << persecond/si.dwNumberOfProcessors << " Hz" << endl;
    cout << "\nCubic time:\n";
    test(maxSubSum1, a, len);
    cout << "\nQuadratic time:\n";
    test(maxSubSum2, a, len);
    cout << "\nN log N time:\n";
    test(maxSubSum3, a, len);
    cout << "\nLinear time:\n";
    test(maxSubSum4, a, len);
    }