VS2005中如何使用QueryPerformanceFrequency()

来源:互联网 发布:ads软件下载 编辑:程序博客网 时间:2024/04/27 20:58


VS2005中如何使用QueryPerformanceFrequency()


VS2005中如何使用QueryPerformanceFrequency() // GetNumbersOfOne.cpp : Defines the entry point for the console application.
// This program used to get the numbers from 0-1234567890 which satisfy following rule:

// f(x) = x which f(x) returns the numbers of 1 for all the numbers from 1 to x. For example:

// f(1) = 1, f(2) = 1, and f(10) = 2, f(11) = 4

#include "stdafx.h"
#include <iostream>
#include <crtdbg.h>
#include <sys/timeb.h>
#include <time.h>
#include <windows.h>

using namespace std;

#define BASE_DECIMAL 10           /* Define the factor of division */
#define LEFT_DECIMAL 1            /* Define the number we will count */
#define MAX_NUMBER  1234567890    /* Define the max number to check */

/* This function used to get the numbers of 1 for a give number */

inline int GetNumbersOfOne(int n)
{
 int count = 0;
 
 do {
  count += ((n % BASE_DECIMAL) == LEFT_DECIMAL);
 } while ((n /= BASE_DECIMAL) > 0);
 
 return count;
}

int _tmain(int argc, _TCHAR* argv[])
{
 int i = 0;
 int count = 0;

 LARGE_INTEGER startCount;
 LARGE_INTEGER endCount;
 LARGE_INTEGER freq;

 QueryPerformanceFrequency(&freq);
 QueryPerformanceCounter(&startCount);
 while (++ i < MAX_NUMBER) {
  count += GetNumbersOfOne(i);
  if (count == i) {
   cout << i << "/t" << count << endl;
  }
 
 }
 QueryPerformanceCounter(&endCount);
 double elapsed;

 elapsed = (double)(endCount.QuadPart - startCount.QuadPart) / freq.QuadPart;
 cout << "Total time elapsed : " << elapsed << endl;

 return 0;
}

 
 
 
 

 

原创粉丝点击