1)在C语言中或者VC中有很多种计算代码执行时间的代码,其中有一种精确计算时间的方法:
__int64 Frequency;
__int64 startTime = 0;
__int64 curTime;
double dTime = 0;
double dTime = 0;
//QueryPerformanceFrequency返回硬件支持的高精度计数器的频率
//返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败
QueryPerformanceFrequency((LARGE_INTEGER *) &Frequency);
QueryPerformanceCounter((LARGE_INTEGER*) &startTime);//开始时间
//需计算执行时间的代码
QueryPerformanceCounter((LARGE_INTEGER*) &curTime);//结束时间
if(startTime> 0 && curTime > 0 )//计算时间,单位是秒
{
dTime = (double)(curTime - startTime) / (double)Frequency;
}
QueryPerformanceFrequency((LARGE_INTEGER *) &Frequency);
QueryPerformanceCounter((LARGE_INTEGER*) &startTime);//开始时间
//需计算执行时间的代码
QueryPerformanceCounter((LARGE_INTEGER*) &curTime);//结束时间
if(startTime> 0 && curTime > 0 )//计算时间,单位是秒
{
dTime = (double)(curTime - startTime) / (double)Frequency;
}
2)#include <time.h> //需要包含的头文件
DWORD begin=0, end=0, cost;
begin = GetTickCount();
//你自己的代码
end = GetTickCount();
cost = (DWORD)(end - begin);//计算以毫秒为单位,精度较差
DWORD begin=0, end=0, cost;
begin = GetTickCount();
//你自己的代码
end = GetTickCount();
cost = (DWORD)(end - begin);//计算以毫秒为单位,精度较差
3)#include<iostream.h>
#include<time.h>
void main()
{
clock_t start,finish;
double totaltime;
start=clock();
…… //把你的程序代码插入到这里面
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"\n此程序的运行时间为"<<totaltime<<"秒!"<<endl;
}