這篇文章主要介紹了C語(yǔ)言時(shí)間處理實(shí)例分享的相關(guān)資料,需要的朋友可以參考下
一、簡(jiǎn)介
時(shí)間處理在編程中經(jīng)常遇到,包括程序的運(yùn)行時(shí)間和顯示時(shí)間等。在標(biāo)準(zhǔn)C中, 日期和時(shí)間的處理包含在 time.h 的頭文件中,需要使用日期和時(shí)間相關(guān)的類型的函數(shù)的話, 需要導(dǎo)入time.h.
二、實(shí)例
1、計(jì)算時(shí)差
- #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);
- //微秒時(shí)差
- 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)行
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
新聞熱點(diǎn)
疑難解答