Jan 19, 2011

c/c++ 时间

计算代码时间,单位毫秒
#ifndef   _CLOCK_T_DEFINED
typedef   long   clock_t;
#define   _CLOCK_T_DEFINED
#endif

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
 clock_t   start,finish;

 start=clock();
 int   i;
 for(i=0;i<10000000;++i)
 {
  //
 }
 finish=clock();

 cout<<"The time that the program costs is: "<<finish-start<<endl;

 return   0;
}


time 计算从四十几年前的某个时刻开始计算的时间,单位为秒
#include <stdio.h>
#include <time.h>

int main(void)
{
   time_t     now;
    
   time(&now);

    
   printf("%d\n", now);

 printf("%f\n years has been past", (double)now/(3600*24*360));
 
  return 0;
}

0 comments: