![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
在VC环境下,如何精确统计函数运行时间
我现在想统计函数的运行时间,现在用的是clock()函数,但是我感觉这个函数精度不够,我想统计很多小的代码片段时,使用clock()函数的统计值为0。请问有什么精度更高的...
我现在想统计函数的运行时间,现在用的是clock()函数,但是我感觉这个函数精度不够,我想统计很多小的代码片段时,使用clock()函数的统计值为0。请问有什么精度更高的计时函数吗?
展开
1个回答
展开全部
有,#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;
}
这个我是翻我的老本本翻到的,很久以前的了,不知道有没有用,试试吧,自己修改下,调整下效率,有其它的要求还可以找我!
![](https://ecmc.bdimg.com/public03/b4cb859ca634443212c22993b0c87088.png)
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询