#include <time.h>
void main(void)
{ clock_t start,end
start = clock(); //记录当前时间
……………… //所要测试的运行程序
end = clock(); //记录当前时间
printf ("The time was:%ld/n", end-start);
//以长整型输出,单位是毫秒(ms)
printf ("The time was:%f/n",(double)(end-start)/CLK_TCK);
//以实型输出,单位是秒(s)
}
其中,关于clock_t和CLK_TCK的说明,在time.h头文件中,如下:
typedef long clock_t;
#define CLOCKS_PER_SEC 1000
#define CLK_TCK CLOCKS_PER_SEC
c语言示例程序如下:
/* 测试用C/C++编写的程序运行的时间 */
#include<time.h>
void main()
{
long i;
long beginTime,endTime;
beginTime=clock();
for(i=0;i<1000;i++)
printf("%ld/t",i);
printf("/n");
endTime=clock();
printf("the beginTime is:%ld/n",beginTime);
printf("the endTime is:%ld/n",endTime);
printf("the runningTime is:%ld/n",endTime-beginTime);
printf("the runningTime is:%f/n",(double)(endTime-beginTime)/1000);
getch();
}