关于GetTickCount函数的用法

来源:互联网 发布:linux程序常驻 编辑:程序博客网 时间:2024/05/01 16:42
#include <windows.h>#include <iostream>using namespace std;int main(){ int a[200][200];  // 数组改大点,不要担心你的宝贝机器会受不了,如果只是区区赋值而已,它眼都不眨一气呵成。 DWORD begin,end,time; begin = GetTickCount();  for(int i =0; i<200; i++) {  for(int j=0; j<200; j++)  {   a[i][j] = i+j;   std::cout << a[i][j] << std:: endl; // 让它一个一个输出,这个可是比较可观的“活儿”  } } end = GetTickCount(); cout<<"begin="<<begin<<endl; cout<<"end="<<end<<endl; time = end - begin; cout<<time; return 0;}
上面是GetTickCount函数最简单的用法,

CPU占用率固定在50%,为一条直线:

int main()

{

    const DWORD busyTime=10;  //设定CPU忙的时间为10ms

    const DWORD idleTime=busyTime;  //CPU空闲的时间与忙的时间相等

    DWORD starTime=0;

    while(1)   //死循环,不关闭程序CPU的占用率则一直保持在50%

    {

        starTime=GetTickCount();  

        while(GetTickCount()-starTime<=busyTime)  //CPU忙的循环

            ;

 

        Sleep(idleTime);  //CPU闲的时间

    }

    return 0;

}

*******************************************************************************************************************
你就当这个函数是得到机器开机后的到现在的毫秒数就行.通常是用在相对时间判断上,典型的例子是用在游戏中,控制每秒钟的帧数.//下面的代码把图像切换控制在33帧/秒int iBegin=GetTickCount();int iNow;while(TRUE){  iNow=GetTickCount();  if (iNow-iBegin<30) continue;  iBegin=iNow;  切换一幅图像.}  
0 0
原创粉丝点击