• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            lynch

            struct timeval和gettimeofday()

            struct timeval結(jié)構(gòu)體在time.h中的定義為: struct timeval { time_t tv_sec; /* Seconds. */ suseconds_t tv_usec; /* Microseconds. */ }; 其中,tv_sec為Epoch到創(chuàng)建struct timeval時(shí)的秒數(shù),tv_usec為微秒

            struct timeval結(jié)構(gòu)體在time.h中的定義為:
            struct timeval
            {
            time_t tv_sec; /* Seconds. */
            suseconds_t tv_usec; /* Microseconds. */
            };
            其中,tv_sec為Epoch到創(chuàng)建struct timeval時(shí)的秒數(shù),tv_usec為微秒數(shù),即秒后面的零頭。比如當(dāng)前我寫博文時(shí)的tv_sec為1244770435,tv_usec為442388,即當(dāng)前時(shí)間距Epoch時(shí)間1244770435秒,442388微秒。需要注意的是,因?yàn)檠h(huán)過程,新建結(jié)構(gòu)體變量等過程需消耗部分時(shí)間,我們作下面的運(yùn)算時(shí)會(huì)得到如下結(jié)果:
            int i;
            for (i = 0; i < 4; ++i)
            {
            gettimeofday(&tv, NULL);
            printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
            sleep(1);
            }
            442388 1244770435
            443119 1244770436
            443543 1244770437
            444153 1244770438
            前面為微秒數(shù),后面為秒數(shù),可以看出,在這個(gè)簡單運(yùn)算中,只能精確到小數(shù)點(diǎn)后面一到兩位,或者可以看出,每進(jìn)行一次循環(huán),均需花費(fèi)0.005秒的時(shí)間,用這個(gè)程序來作計(jì)時(shí)器顯然是不行的,除非精確計(jì)算產(chǎn)生的代碼消耗時(shí)間。

            gettimeofday() -- 獲取當(dāng)前時(shí)間(保存在結(jié)構(gòu)體timeval中)

            #include <stdio.h>
            #include <sys/time.h>
            #include <time.h>

            int main(int argc, char * argv[]){

            struct timeval tv; //(1)
            while(1){
            gettimeofday(&tv, NULL); //(2)
            printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
            sleep(2);
            }
            return 0;

            }

            (1)
            struct--timeval
            --------------------------------------------------
            struct timeval {
            time_t tv_sec; /* seconds */
            suseconds_t tv_usec; /* microseconds */
            };
            millisecond 毫秒
            microsecond 微秒

            timeval表示一個(gè)時(shí)間點(diǎn),比如:
            timeval.tv_sec = 1 (s)
            timevat.tv_usec = 500 000 (
            μs)
            1:500 = 1s500000
            μs = 1.5s

            (2) gettimeofday()
            --------------------------------------------------
            int gettimeofday(struct timeval *tv, struct timezone *tz);

            The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
            The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.

            (3) 運(yùn)行結(jié)果:
            --------------------------------------------------
            time 1181788367:991487
            time 1181788369:991602

            表示睡眠2秒經(jīng)過的精確時(shí)間為: 2s115
            μs


            posted on 2011-08-05 10:50 董三 閱讀(42359) 評論(0)  編輯 收藏 引用 所屬分類: C++

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            博客友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            怡红院日本一道日本久久 | 久久综合九色综合久99| 一本伊大人香蕉久久网手机| 国产成人精品久久二区二区| 国产精品激情综合久久| 无码人妻久久一区二区三区蜜桃 | 久久精品国产WWW456C0M| 伊人 久久 精品| 成人免费网站久久久| 久久露脸国产精品| 无码超乳爆乳中文字幕久久| 狠狠综合久久综合中文88| 伊人久久精品无码二区麻豆| 精品久久久久久无码人妻热| 人妻久久久一区二区三区| 久久精品国产亚洲7777| 国产V综合V亚洲欧美久久 | 久久国产精品无码一区二区三区| 国产91色综合久久免费| 亚洲国产婷婷香蕉久久久久久| 久久超碰97人人做人人爱| 亚州日韩精品专区久久久| 亚洲国产精品久久久久网站| 亚洲精品乱码久久久久久久久久久久| 久久艹国产| 办公室久久精品| 狠狠色丁香久久综合婷婷| 蜜臀久久99精品久久久久久小说| 久久综合色区| 久久亚洲国产精品123区| 久久91亚洲人成电影网站| 久久久精品2019免费观看| 精品国产乱码久久久久久人妻| 婷婷久久综合| 伊人久久大香线蕉亚洲五月天| 日本亚洲色大成网站WWW久久| 国产AV影片久久久久久| 国产精品女同一区二区久久| 日本福利片国产午夜久久| 狠狠色丁香久久综合五月| 97精品国产97久久久久久免费 |