这几个是我一个个试出来的,很好用的。呵呵,希望对你有用!
/*clock计算调用进程使用处理器的时间
函数原型:clock_t clock(void)*/
//实例
//******C语言描述******
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sleep(clock_t wait);
int main(void)
{
long i=6000000L;
clock_t start,finish;
double duration;
printf("Delay for three seconds/n");
sleep((clock_t)3*CLOCKS_PER_SEC);
printf("Done!/n");
printf("Time to do %ld empty loops is",i);
start=clock();
while(i--);
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%2.lf seconds/n",duration);
}
void sleep(clock_t wait)
{
clock_t goal;
goal=wait+clock();
while(goal>clock());
}
//******C++语言描述******
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void sleep(clock_t wait);
int main(void)
{
long i=6000000L;
clock_t start,finish;
double duration;
cout<<"Delay for three seconds/n";
sleep((clock_t)3*CLOCKS_PER_SEC);
cout<<"Done!/n";
cout<<"Time to do "<<i <<" empty loops is ";
start=clock();
while(i--);
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<duration<<" seconds/n";
}
void sleep(clock_t wait)
{
clock_t goal;
goal=wait+clock();
while(goal>clock());
}
//******运行结果******
Delay for three seconds
Done!
Time to do 6000000 empty loops is 0.02 seconds
Press any key to continue
//difftime计算t1,t2两个时间差
//函数原型:double difftime(time_t t1,time_t t2);t1开始时间 ,t2结束时间
//实例:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
time_t start,finish;
long loop;
double result,elapsed_time;
cout<<"Multiplying 2 floating point number 500 million times ..../n";
time(&start);
for(loop=0;loop<500000000;loop++)
result=3.63*5.27;
time(&finish);
elapsed_time=difftime(finish,start);
cout<<"Program takes "<<elapsed_time <<" seconds./n";
return 0;
}
//******运行结果******
Multiplying 2 floating point number 500 million times ....
Program takes 2 seconds.
Press any key to continue
//asctime把时间日期转换为字符串
//函数原型:char *asctime(const struct tm *timeptr);timeptr时间日期结构指针
//实例:
//#include <iostream>
#include <time.h>
#include <stdio.h>
//using namespace std;
struct tm *newtime;
time_t aclock;
int main(void)
{
time(&aclock);
newtime=localtime(&aclock);
printf("current data and time %s",asctime(newtime));
//return 0;
}
//******运行结果******
current data and time Sun Jul 01 17:23:49 2007
Press any key to continue
2010.12.22-time.h包含的库函数 一些常用的时间函数
最新推荐文章于 2020-05-08 11:37:29 发布