• <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結構體在time.h中的定義為: struct timeval { time_t tv_sec; /* Seconds. */ suseconds_t tv_usec; /* Microseconds. */ }; 其中,tv_sec為Epoch到創(chuàng)建struct timeval時的秒數,tv_usec為微秒

            struct timeval結構體在time.h中的定義為:
            struct timeval
            {
            time_t tv_sec; /* Seconds. */
            suseconds_t tv_usec; /* Microseconds. */
            };
            其中,tv_sec為Epoch到創(chuàng)建struct timeval時的秒數,tv_usec為微秒數,即秒后面的零頭。比如當前我寫博文時的tv_sec為1244770435,tv_usec為442388,即當前時間距Epoch時間1244770435秒,442388微秒。需要注意的是,因為循環(huán)過程,新建結構體變量等過程需消耗部分時間,我們作下面的運算時會得到如下結果:
            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
            前面為微秒數,后面為秒數,可以看出,在這個簡單運算中,只能精確到小數點后面一到兩位,或者可以看出,每進行一次循環(huán),均需花費0.005秒的時間,用這個程序來作計時器顯然是不行的,除非精確計算產生的代碼消耗時間。

            gettimeofday() -- 獲取當前時間(保存在結構體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表示一個時間點,比如:
            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) 運行結果:
            --------------------------------------------------
            time 1181788367:991487
            time 1181788369:991602

            表示睡眠2秒經過的精確時間為: 2s115
            μs


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

            <2012年12月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導航

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            博客友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品免费看国产免费| 2021久久精品免费观看| 色婷婷久久综合中文久久蜜桃av| 久久亚洲精品国产亚洲老地址 | 久久精品无码一区二区日韩AV| 欧美久久天天综合香蕉伊| 亚洲天堂久久久| 91精品国产9l久久久久| 久久久这里只有精品加勒比| 99久久精品国产麻豆| 香港aa三级久久三级老师2021国产三级精品三级在| 久久久国产99久久国产一| 99久久国产综合精品网成人影院| 午夜精品久久久久久影视777| 久久久无码精品亚洲日韩蜜臀浪潮| 曰曰摸天天摸人人看久久久| 久久国产欧美日韩精品| 性高湖久久久久久久久AAAAA| 久久国产乱子伦免费精品| 一级a性色生活片久久无| 天天综合久久久网| 99久久成人国产精品免费| 成人久久免费网站| 久久久无码精品亚洲日韩软件| 亚洲嫩草影院久久精品| 久久久久久亚洲Av无码精品专口| 色狠狠久久综合网| 日本精品久久久久久久久免费| 久久精品国内一区二区三区| 丰满少妇高潮惨叫久久久| 99久久夜色精品国产网站 | 国产精品久久久久久久久软件| 国产精品99久久久久久www| 精品国产91久久久久久久| 久久狠狠高潮亚洲精品| 久久婷婷五月综合色高清| 久久久久亚洲AV无码网站| 狠狠狠色丁香婷婷综合久久俺| 久久综合九色综合欧美狠狠| 久久久精品免费国产四虎| 亚洲天堂久久精品|