有,#include <windows.h>,包含这个头文件。
DWORD Begin = GetTickCount();//在你需要计时的时候调用这个。
要结束的时候调用:
DWORD Gap = GetTickCount() - Begin;
这里的Gap是毫秒为单位,如果为1000,表示1000毫秒,1秒。
你可以自己封装一下这个功能自定义一个精确计时器。
比如这样一个:
#include <windows.h>
#include <iostream>
using namespace std;
class CTime
{
public:
CTime();
~CTime();
private:
DWORD tPre;
DWORD tNow;
public:
void restore();
DWORD getDelay();
private:
void SetPre();
void SetNow();
};
CTime::CTime():tPre(0),tNow(0){}
CTime::~CTime(){ }
void CTime::SetPre()
{
tPre = GetTickCount();
}
void CTime::SetNow()
{
tNow = GetTickCount();
}
void CTime::restore()
{
SetPre();
SetNow();
}
DWORD CTime::getDelay()
{
SetNow();
return tNow-tPre;
}
int main()
{
CTime time;
time.restore();
int n = 0;
while(true)
{
if(time.getDelay()>1000)
{
n++;
time.restore();
}
system("cls");
cout<<" "<<n<<endl;
}
return 0;
}
DWORD Begin = GetTickCount();//在你需要计时的时候调用这个。
要结束的时候调用:
DWORD Gap = GetTickCount() - Begin;
这里的Gap是毫秒为单位,如果为1000,表示1000毫秒,1秒。
你可以自己封装一下这个功能自定义一个精确计时器。
比如这样一个:
#include <windows.h>
#include <iostream>
using namespace std;
class CTime
{
public:
CTime();
~CTime();
private:
DWORD tPre;
DWORD tNow;
public:
void restore();
DWORD getDelay();
private:
void SetPre();
void SetNow();
};
CTime::CTime():tPre(0),tNow(0){}
CTime::~CTime(){ }
void CTime::SetPre()
{
tPre = GetTickCount();
}
void CTime::SetNow()
{
tNow = GetTickCount();
}
void CTime::restore()
{
SetPre();
SetNow();
}
DWORD CTime::getDelay()
{
SetNow();
return tNow-tPre;
}
int main()
{
CTime time;
time.restore();
int n = 0;
while(true)
{
if(time.getDelay()>1000)
{
n++;
time.restore();
}
system("cls");
cout<<" "<<n<<endl;
}
return 0;
}