一、簡介
時間處理在編程中經(jīng)常遇到,包括程序的運(yùn)行時間和顯示時間等。在標(biāo)準(zhǔn)C中, 日期和時間的處理包含在 time.h 的頭文件中,需要使用日期和時間相關(guān)的類型的函數(shù)的話, 需要導(dǎo)入time.h.
二、實(shí)例
1、計算時差
#include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ struct timeval start, end; unsigned long spend_time; gettimeofday( &start, NULL ); printf("start : %d.%d/n", start.tv_sec, start.tv_usec); sleep(1); gettimeofday( &end, NULL ); printf("end : %d.%d/n", end.tv_sec, end.tv_usec); //微秒時差 spend_time=1000000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec); printf("%ld/n",spend_time); return 0;}
編譯
gcc -g -o time_diff time_diff.c
運(yùn)行
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
新聞熱點(diǎn)
疑難解答
圖片精選