• <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>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            使用API實(shí)現(xiàn)1/1000秒的性能測試類

            ?

            // ?MbUnit?Test?Framework
            // ?
            // ?Copyright?(c)?2004?Jonathan?de?Halleux
            //
            // ?This?software?is?provided?'as-is',?without?any?express?or?implied?warranty.?
            // ?
            // ?In?no?event?will?the?authors?be?held?liable?for?any?damages?arising?from?
            // ?the?use?of?this?software.
            // ?Permission?is?granted?to?anyone?to?use?this?software?for?any?purpose,?
            // ?including?commercial?applications,?and?to?alter?it?and?redistribute?it?
            // ?freely,?subject?to?the?following?restrictions:
            //
            // ????????1.?The?origin?of?this?software?must?not?be?misrepresented;?
            // ????????you?must?not?claim?that?you?wrote?the?original?software.?
            // ????????If?you?use?this?software?in?a?product,?an?acknowledgment?in?the?product?
            // ????????documentation?would?be?appreciated?but?is?not?required.
            //
            // ????????2.?Altered?source?versions?must?be?plainly?marked?as?such,?and?must?
            // ????????not?be?misrepresented?as?being?the?original?software.
            //
            // ????????3.?This?notice?may?not?be?removed?or?altered?from?any?source?
            // ????????distribution.
            // ????????
            // ????????MbUnit?HomePage:? http://mbunit.tigris.org
            // ????????Author:?Jonathan?de?Halleux

            using ?System;
            using ?System.Runtime.InteropServices;
            using ?System.ComponentModel;
            using ?System.Threading;

            namespace ?MbUnit.Core.Monitoring
            {
            ????
            /// ? <summary>
            ????
            /// ?A?high?performance?timer
            ????
            /// ? </summary>
            ????
            /// ? <remarks>
            ????
            /// ?High?Precision?Timer?based?on?Win32?methods.
            ????
            /// ? </remarks>
            ????
            /// ? <example>
            ????
            /// ?This?example?times?the?execution?of?a?method:
            ????
            /// ? <code>
            ????
            /// ?TimeMonitor?timer?=?new?TimeMonitor();
            ????
            /// ?timer.Start();
            ????
            /// ?????//?execute?code
            ????
            /// ?timer.Stop();
            ????
            /// ?
            ????
            /// ?Console.WriteLine("Duration:?{0}",timer.Duration);
            ????
            /// ? </example>

            ???? public ? class ?TimeMonitor
            ????
            {
            ????????[DllImport(
            " Kernel32.dll " )]
            ????????
            private ? static ? extern ? bool ?QueryPerformanceCounter( out ? long ?lpPerformanceCount);??

            ????????[DllImport(
            " Kernel32.dll " )]
            ????????
            private ? static ? extern ? bool ?QueryPerformanceFrequency( out ? long ?lpFrequency);
            ????????
            ????????
            private ? long ?startTime,?stopTime;
            ????????
            private ? long ?now;
            ????????
            private ? long ?freq;
            ????????
            ????????
            /// ? <summary> Default?constructor </summary>
            ????????
            /// ? <remarks> Initializes?the?timer. </remarks>

            ???????? public ?TimeMonitor()
            ????????
            {
            ????????????startTime?
            = ? 0 ;
            ????????????stopTime??
            = ? 0 ;

            ????????????
            if ?(QueryPerformanceFrequency( out ?freq)? == ? false )
            ????????????
            {
            ????????????????
            // ?high-performance?counter?not?supported?
            ???????????????? throw ? new ?Win32Exception();?
            ????????????}

            ????????}

            ????????
            ????????
            /// ? <summary> Gets?the?clock?frequency </summary>
            ????????
            /// ? <value> Clock?frequency </value>

            ???????? public ? long ?Frequency
            ????????
            {
            ????????????
            get
            ????????????
            {
            ????????????????
            return ? this .freq;
            ????????????}

            ????????}

            ????????
            ????????
            /// ? <summary> Starts?the?timer </summary>
            ????????
            /// ? <remarks> Resets?the?duration?and?starts?the?timer </remarks>

            ???????? public ? void ?Start()
            ????????
            {
            ????????????
            // ?lets?do?the?waiting?threads?there?work
            ????????????Thread.Sleep( 0 );??

            ????????????QueryPerformanceCounter(
            out ?startTime);
            ????????}

            ????????
            ????????
            /// ? <summary> Stops?the?timer </summary>
            ????????
            /// ? <remarks> Stops?the?timer </remarks>

            ???????? public ? void ?Stop()
            ????????
            {
            ????????????QueryPerformanceCounter(
            out ?stopTime);
            ????????}

            ????????
            ????????
            /// ? <summary> Gets?the?current?duration?value?without?stopping?the?timer </summary>
            ????????
            /// ? <value> Current?duration?value </value>

            ???????? public ? double ?Now
            ????????
            {
            ????????????
            get
            ????????????
            {
            ????????????????QueryPerformanceCounter(
            out ?now);
            ????????????????
            return ?( double )(now? - ?startTime)? / ?( double )?freq;
            ????????????}
            ????????
            ????????}


            ????????
            /// ? <summary> Gets?the?timed?duration?value?in?seconds </summary>
            ????????
            /// ? <value> Timer?duration </value>

            ???????? public ? double ?Duration
            ????????
            {
            ????????????
            get
            ????????????
            {
            ????????????????
            return ?( double )(stopTime? - ?startTime)? / ?( double )?freq;
            ????????????}

            ????????}

            ????}

            }




            c++版的:
            High-resolution?timer?for?timing?code?fragments?


            Timing?of?code?fragments?
            is?simple:?

            record?the?start?time?
            execute?the?code?
            record?the?end?time?
            calucate?the?difference?between?end?and?start?
            For?timing?exuction?time?of?pieces?of?code?you?need?a?high
            -resolution?timer.?On?Windows,?Windows?CE?and?Pocket?PC/Smartphone?(which?are?Windows?CE?variations)?you?can?use?QueryPerformanceCounter?and?QueryPerformanceFrequence?API?calls.?

            Here
            's?a?simple?implementation?in?C:?

            typedef?
            struct?prof_timer_t?{
            ????LARGE_INTEGER?time_start;
            ????LARGE_INTEGER?time_stop;
            }
            ?prof_timer_t;

            void?prof_timer_start(prof_timer_t?*timer)?{
            ????QueryPerformanceCounter(
            &timer->time_start);
            }


            void?prof_timer_stop(prof_timer_t?*timer)?{
            ????QueryPerformanceCounter(
            &timer->time_stop);
            }


            double?prof_timer_get_duration_in_secs(prof_timer_t?*timer)?{
            ????LARGE_INTEGER?freq;
            ????
            double?duration;
            ????QueryPerformanceFrequency(
            &freq);
            ????duration?
            =?(double)(timer->time_stop.QuadPart-timer->time_start.QuadPart)/(double)freq.QuadPart;
            ????
            return?duration;
            }


            And?
            in?C++:?

            //?very?simple,?high-precision?(at?least?in?theory)?timer?for?timing?API?calls
            struct?ProfTimer?{
            ????
            void?Start(void)?{
            ????????QueryPerformanceCounter(
            &mTimeStart);
            ????}
            ;
            ????
            void?Stop(void)?{
            ????????QueryPerformanceCounter(
            &mTimeStop);
            ????}
            ;
            ????
            double?GetDurationInSecs(void)
            ????
            {
            ????????LARGE_INTEGER?freq;
            ????????QueryPerformanceFrequency(
            &freq);
            ????????
            double?duration?=?(double)(mTimeStop.QuadPart-mTimeStart.QuadPart)/(double)freq.QuadPart;
            ????????
            return?duration;
            ????}


            ????LARGE_INTEGER?mTimeStart;
            ????LARGE_INTEGER?mTimeStop;
            }
            ;

            And?here
            's?an?example?of?using?the?C++?version:?

            ????ProfTimer?t;
            ????t.Start();
            ????foo();
            ????t.Stop();
            ????
            double?dur?=?t.GetDurationInSecs();
            ????printf(
            "executing?foo()?took?%f?seconds\n"?,?dur);

            posted on 2006-06-16 14:07 夢在天涯 閱讀(1726) 評(píng)論(1)  編輯 收藏 引用 所屬分類: CPlusPlusC#/.NETVS2005/2008

            評(píng)論

            # re: 性能測試類 2006-06-16 14:26 夢在天涯

            GetTickCount()和GetCurrentTime()都只精確到55ms(1個(gè)tick就是55ms)。如果要精確到毫秒,應(yīng)該使用timeGetTime函數(shù)或QueryPerformanceCounter函數(shù)。
            雖然timeGetTime返回值的單位是1ms,但實(shí)際上它的精度只有10ms左右。
            如果想提高精度,可以使用QueryPerformanceCounter和QueryPerformanceFrequency。這兩個(gè)函數(shù)不是在每個(gè)系統(tǒng)中都支持。對(duì)于支持它們的系統(tǒng)中,可以獲得低于1ms的精度。Windows 內(nèi)部有一個(gè)精度非常高的定時(shí)器, 精度在微秒級(jí), 但不同的系統(tǒng)這個(gè)定時(shí)器的頻率不同, 這個(gè)頻率與硬件和操作系統(tǒng)都可能有關(guān)。利用 API 函數(shù) QueryPerformanceFrequency 可以得到這個(gè)定時(shí)器的頻率。利用 API 函數(shù) QueryPerformanceCounter 可以得到定時(shí)器的當(dāng)前值。根據(jù)要延時(shí)的時(shí)間和定時(shí)器的頻率, 可以算出要延時(shí)的時(shí)間定時(shí)器經(jīng)過的周期數(shù)。在循環(huán)里用 QueryPerformanceCounter 不停的讀出定時(shí)器值, 一直到經(jīng)過了指定周期數(shù)再結(jié)束循環(huán), 就達(dá)到了高精度延時(shí)的目的。  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1808095
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久精品国产亚洲AV忘忧草18| 久久久久国产一区二区| 久久99热这里只频精品6| 久久国产视频网| 久久99九九国产免费看小说| 蜜臀av性久久久久蜜臀aⅴ| 久久精品人人槡人妻人人玩AV| 潮喷大喷水系列无码久久精品| 国产高潮国产高潮久久久91 | 精品久久久久久国产| 久久不见久久见免费影院www日本| 色综合久久天天综线观看| 国内精品久久久人妻中文字幕| 成人亚洲欧美久久久久| 国产成人久久精品一区二区三区| 久久美女人爽女人爽| 久久中文字幕人妻丝袜| 嫩草影院久久国产精品| 午夜天堂av天堂久久久| 久久精品国产只有精品66| 粉嫩小泬无遮挡久久久久久| 久久综合久久伊人| 2021国产成人精品久久| 久久久久人妻一区精品色| 手机看片久久高清国产日韩| 久久综合综合久久狠狠狠97色88| 亚洲AV无码1区2区久久| 人人狠狠综合久久亚洲高清| 久久精品国产99国产电影网| 伊人久久大香线蕉亚洲五月天 | 久久99热这里只频精品6| 久久午夜电影网| 大伊人青草狠狠久久| 国内精品人妻无码久久久影院| 久久久久青草线蕉综合超碰| 亚洲v国产v天堂a无码久久| 国内精品久久久久久久亚洲 | 无码任你躁久久久久久老妇App| 精品久久久久中文字| 国产成人香蕉久久久久| 国产精品免费久久久久久久久|