C++中如何求出程序的运行时间

来源:互联网 发布:安卓端口映射软件 编辑:程序博客网 时间:2024/06/06 12:38
C++中的基石函数是clock(),而与其相关的数据类型是clock_t(头文件time.h)。函数原型:clock_t clock(void);
   在time.h中对clock_t的定义为:
  #ifndef _CLOCK_T_DEFINED
   typedef long clock_t;
   #define _CLOCK_T_DEFINED
   #endif
  下面举例告诉你
   #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;
  }


  如此可以求出程序运行的大概时间
0 0
原创粉丝点击