編程實現時間差,推薦兩個方法。
1、time()函數
計時精確到ms級。
2、clock()函數
計時精確到s級。
兩個函數都應包含頭文件:time.h。
代碼如下:
//代碼表示的是從程序開始運行到鍵盤按鍵反應這個時間差。
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
time_t c_start,t_start, c_end,t_end;
c_start = clock();
t_start = time(NULL) ;
system("pause") ; //按鍵反應,包含在頭文件stdlib.h中。
c_end = clock();
t_end = time(NULL) ;
printf("The pause used %f ms by time().\n",difftime(c_end,c_start)) ;
printf("The pause used %f s by clock().\n",difftime(t_end,t_start)) ;
system("pause");
return 0;
}