• <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>
            posts - 71,  comments - 41,  trackbacks - 0

            System Clock級高精度計時器,支持Window/Linux平臺。輸入參數CPU主頻,該值可從注冊表(Windows)或/proc/cpuinfo(Linux)中獲得。
            大道至簡——精度高,抖動大

            // ?*****************************************************************************
            // ??IA32Timer???version:??1.0????date:?04/26/2006
            // ??----------------------------------------------------------------------------
            // ??This?class?is?a?wrapper?of?IA32?RDTSC?
            // ??(ReaD?Time?Stamp?Counter)?instruction.
            // ??It?will?help?you?get?the?interval?time?with
            // ??different?level.
            // ??----------------------------------------------------------------------------
            // ??Copyright?(C)?2006?-?Charles?Zu
            // ?*****************************************************************************
            // ?????Note:?All?the?classes?the?author?designed?are?leading?with?"Z"
            // ??which?is?contradistinguished?form?the?leading?with?"C"?of?MFC
            // ?*****************************************************************************

            class ?ZIA32Timer
            {
            public :
            ????ZIA32Timer(
            double ?ghz)
            ????:m_startcycle(
            0 ),?m_ghz(ghz)
            ????
            {
            #ifndef?_WIN32
            ????????m_high?
            = ? 0 ;
            ????????m_low?
            = ? 0 ;
            #endif
            ????}


            ????
            void ?Start()
            ????
            {
            #ifdef?_WIN32
            ????????m_startcycle?
            = ?RTSC();
            #else
            ????????RTSC();
            ????????m_startcycle?
            = ?(unsigned? long ? long )?m_high? * ?( 1 ? << ? 30 )? * ? 4 ? + ?m_low;
            #endif
            ????}


            #ifdef?_WIN32
            ????unsigned?__int64?Stop(
            int ?unit);
            #else
            ????unsigned?
            long ? long ?Stop( int ?unit);
            #endif

            ????
            static ? enum ?Unit {CYCLE,?NS,?US,?MS,?S} s_unit;

            private :
            #ifdef?_WIN32
            ????unsigned?__int64??m_startcycle;
            #else
            ????unsigned?
            long ? long ?m_startcycle;
            ????unsigned?
            int ?m_high;
            ????unsigned?
            int ?m_low;
            #endif

            #ifdef?_WIN32
            ????unsigned?__int64?RTSC();
            #else
            ???
            void ?RTSC();
            #endif

            double ?m_ghz;

            }
            ;

            #ifdef?_WIN32
            ????inline?unsigned?__int64?ZIA32Timer::RTSC()
            ????
            {
            ????????_asm????_emit?
            0x0F
            ????????_asm????_emit?
            0x31
            ????}

            #else
            ????inline?
            void ?ZIA32Timer::RTSC()
            ????
            {
            ????????asm(
            " rdtsc;?movl?%%edx,%0;?movl?%%eax,%1 " ??? // ?Read?cycle?counter
            ????????????:? " =r " ?(m_high),? " =r " ?(m_low)????????????????????????????????????
            ????????????:?
            /* ?No?input? */ ?????????????????????????????
            ????????????:?
            " %edx " ,? " %eax " );
            ????}

            #endif

            #ifdef?_WIN32
            ????inline?unsigned?__int64?ZIA32Timer::Stop(
            int ?unit)
            ????
            {
            ????????
            switch (unit)
            ????????
            {
            ????????
            case ?CYCLE:
            ????????????
            return ?(unsigned?__int64)(RTSC()? - ?m_startcycle);
            ????????
            break ;

            ????????
            case ?NS:
            ????????????
            return ?(unsigned?__int64)((RTSC()? - ?m_startcycle)? / ?m_ghz);
            ????????
            break ;

            ????????
            case ?US:
            ????????????
            return ?(unsigned?__int64)((RTSC()? - ?m_startcycle)? / ?m_ghz? / ? 1000 );
            ????????
            break ;

            ????????
            case ?MS:
            ????????????
            return ?(unsigned?__int64)((RTSC()? - ?m_startcycle)? / ?m_ghz? / ? 1000000 );
            ????????
            break ;

            ????????
            case ?S:
            ????????????
            return ?(unsigned?__int64)((RTSC()? - ?m_startcycle)? / ?m_ghz? / ? 1000000000 );
            ????????
            break ;

            ????????
            default :
            ????????????
            break ;
            ????????}

            ????????
            return ? 0 ;
            ????}

            #else
            ????inline?unsigned?
            long ? long ?ZIA32Timer::Stop( int ?unit)
            ????
            {
            ????????unsigned?
            long ? long ?stoppiont;
            ????????RTSC();
            ????????stoppiont?
            = ?(((unsigned? long ? long )m_high)? << ? 32 )? + ?m_low;
            ????????
            ????????
            switch (unit)
            ????????
            {
            ????????
            case ?CYCLE:
            ????????????
            return ?(unsigned? long ? long )(stoppiont? - ?m_startcycle);
            ????????????
            break ;

            ????????
            case ?NS:
            ????????????
            return ?(unsigned? long ? long )((stoppiont? - ?m_startcycle)? / ?m_ghz);
            ????????????
            break ;

            ????????
            case ?US:
            ????????????
            return ?(unsigned? long ? long )((stoppiont? - ?m_startcycle)? / ?m_ghz? / ? 1000 );
            ????????????
            break ;

            ????????
            case ?MS:
            ????????????
            return ?(unsigned? long ? long )((stoppiont? - ?m_startcycle)? / ?m_ghz? / ? 1000000 );
            ????????????
            break ;

            ????????
            case ?S:
            ????????????
            return ?(unsigned? long ? long )((stoppiont? - ?m_startcycle)? / ?m_ghz? / ? 1000000000 );
            ????????????
            break ;

            ????????
            default :
            ????????????
            break ;
            ????????}

            ????????
            return ? 0 ;
            ????}

            #endif

            下邊給個測試程序,由于我的CPU主頻為3GHZ,所以寫了hard code
            #include?<stdio.h>
            #ifdef?_WIN32
            #include?
            <windows.h>
            #else
            #include?
            <unistd.h>
            #endif

            #include?
            "IA32Timer.h"

            void?MySleep()
            {
            #ifdef?_WIN32
            //?????Sleep(1);
            ?????Sleep(10);
            //?????Sleep(100);
            //?????Sleep(1000);
            #else
            ????usleep(
            100?*?1000);
            #endif
            }


            int?main(int?argc,?char**?argv)
            {
            ????ZIA32Timer?ztimer(
            3);?//the?arg?is?the?GHZ?of?your?CPU

            #ifdef?_WIN32
            unsigned?__int64?interval;
            #else
            unsigned?
            long?long?interval;
            #endif

            ????ztimer.Start();
            ????MySleep();
            ????interval?
            =?ztimer.Stop(ZIA32Timer::CYCLE);
            ????fprintf(stdout,?
            "interval?=?%u?(cycle)\n",?interval);

            ????ztimer.Start();
            ????MySleep();
            ????interval?
            =?ztimer.Stop(ZIA32Timer::NS);
            ????fprintf(stdout,?
            "interval?=?%u?(ns)\n",?interval);

            ????ztimer.Start();
            ????MySleep();
            ????interval?
            =?ztimer.Stop(ZIA32Timer::US);
            ????fprintf(stdout,?
            "interval?=?%u?(us)\n",?interval);

            ????ztimer.Start();
            ????MySleep();
            ????interval?
            =?ztimer.Stop(ZIA32Timer::MS);
            ????fprintf(stdout,?
            "interval?=?%u?(ms)\n",?interval);

            ????ztimer.Start();
            ????MySleep();
            ????interval?
            =?ztimer.Stop(ZIA32Timer::S);
            ????fprintf(stdout,?
            "interval?=?%u?(s)\n",?interval);

            ????
            return?0;
            }

            參考了高人的匯編實現,自己整理,貼出來,希望對你有啟示
            posted on 2006-11-28 14:49 Charles 閱讀(1750) 評論(4)  編輯 收藏 引用 所屬分類: Helper Utility

            FeedBack:
            # re: IA32/Windows&Linux高精度計時器
            2006-11-28 18:55 | erran
            看的不是很明白, 不過
            win32下有精確計時的API:
            QueryPerformanceFrequency
            QueryPerformanceCounter
            也是Cpu級的, 用起來很方便.
            linux應該也有這樣的系統API..
              回復  更多評論
              
            # re: IA32/Windows&Linux高精度計時器
            2006-11-28 19:48 | Charles
            IA32's RDTSC(ReaD Time Stamp Counter) instruction returns the number of clock cycles passed since the booting of the CPU in 64-bit unsigned integer, through the EDX and EAX 32-bit general register pair. So it is more precision than any other method:)  回復  更多評論
              
            # re: IA32/Windows&Linux高精度計時器
            2007-12-17 19:15 | input
            如果是雙核cpu會對測試結果產生什么影響?  回復  更多評論
              
            # re: IA32/Windows&Linux高精度計時器
            2008-01-08 11:52 | 小林子
            在采用訊馳技術的筆記本芯片上,使用 RTDSC 指令不可信,因為CPU 是變頻的  回復  更多評論
              
            <2007年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            決定開始寫工作日記,記錄一下自己的軌跡...

            常用鏈接

            留言簿(4)

            隨筆分類(70)

            隨筆檔案(71)

            charles推薦訪問

            搜索

            •  

            積分與排名

            • 積分 - 50763
            • 排名 - 448

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合久久自在自线精品自 | 无码日韩人妻精品久久蜜桃| 久久影院午夜理论片无码| 亚洲人成无码网站久久99热国产| 尹人香蕉久久99天天拍| 狠狠精品久久久无码中文字幕| 亚洲中文字幕无码久久2020| 日本免费久久久久久久网站| 亚洲一级Av无码毛片久久精品| 久久久久久久久波多野高潮| 久久91亚洲人成电影网站| 亚洲人成网站999久久久综合| 99久久综合狠狠综合久久止| 欧美午夜A∨大片久久| 久久久一本精品99久久精品66| 久久AAAA片一区二区| 国产精品免费看久久久| 色8激情欧美成人久久综合电| 久久精品亚洲一区二区三区浴池 | 久久精品国产一区| 久久经典免费视频| 久久久久亚洲AV综合波多野结衣| 久久久久国产精品熟女影院| 久久精品成人欧美大片| 欧洲性大片xxxxx久久久| 91精品无码久久久久久五月天| 一本色综合网久久| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 国产一级持黄大片99久久| 久久精品国产2020| 亚洲精品乱码久久久久久不卡| www亚洲欲色成人久久精品| 久久99国产精品99久久| 国产精品18久久久久久vr | 91秦先生久久久久久久| 久久w5ww成w人免费| 久久婷婷五月综合色奶水99啪| 少妇久久久久久久久久| 久久超乳爆乳中文字幕| 久久91精品国产91久久麻豆| 日本免费一区二区久久人人澡|