您的位置:首页 > 其它

如何测试程序的执行时间

2009-08-02 14:43 495 查看
1. #include   "time.h"  

   #include   "stdio.h"  

   #include   "conio.h"   

 int  main()  

  {  

      time_t   start,end;  

      start=time(NULL);  

      {你所要测试的程序}  

      end=time(NULL);  

      printf("%6.3f/n",difftime(end,start));  

      getch();  

  }

 

2.   time_t   start,end;  

      start=time(NULL);  

      system("test.exe");     //注意将test.exe拷贝到程序路径下  

      end=time(NULL);  

      printf("%6.3f/n",difftime(end,start));

 

3. linux下:

  命令提示符输入  

  $   time   hello  

  执行结束,就会输出该程序耗时几许

 

4. 

  #include   <time.h>  

  int   main(void)  

  {  

  long   a;        

  char   pnam[80]={0};  

   

  puts("输入程序名:");  

  gets(pnam);  

   

  a=clock();  

  system(pnam);  

  printf("/n   程序执行总时间为:%ld   (1/1000秒)/n",(clock()-a));  

  getch();  

  return   0;  

  }  

 

5.Linux 下:

#include  <stdio.h>

   #include <stdlib.h>

   #include <math.h>

   #include <unistd.h>

   #include <time.h>

   #include <sys/time.h>

 

 void fun()

 {

     int i,j;

    for(j=0;j<300;j++)

    for(i=0;i<10000;i++)

        //printf("%lf/n", sin(i));

        sin(i);

 }

 int  main() 

  { 

      struct timeval tpstart,tpend;

      double timeuse;

      gettimeofday(&tpstart,NULL);

      {

    fun();

      }

     

     gettimeofday(&tpend,NULL);

     timeuse = 1e6*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

     timeuse /= 1e6;

      printf("%lf/n",timeuse); 

     exit(0);

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息