這幾個是我一個個試出來的,很好用的。呵呵,希望對你有用!
/*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包含的庫函數 一些常用的時間函數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.