• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            使用API實現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 夢在天涯 閱讀(1720) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlusC#/.NET 、VS2005/2008

            評論

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

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

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            欧美日韩精品久久久久| 婷婷伊人久久大香线蕉AV | 草草久久久无码国产专区| 久久精品三级视频| 精品欧美一区二区三区久久久 | 少妇人妻综合久久中文字幕| 国产亚洲精久久久久久无码AV| 久久综合精品国产二区无码| 久久婷婷午色综合夜啪| 婷婷国产天堂久久综合五月| 尹人香蕉久久99天天拍| 亚洲国产综合久久天堂| 思思久久好好热精品国产| 99久久香蕉国产线看观香| 狠狠精品久久久无码中文字幕 | 国产成人精品综合久久久| 狼狼综合久久久久综合网| 精品久久久久久久无码| 日韩亚洲欧美久久久www综合网| 欧美伊香蕉久久综合类网站| 国产激情久久久久影院小草| 久久久久久毛片免费看 | 综合久久一区二区三区 | 国产∨亚洲V天堂无码久久久| 人妻少妇久久中文字幕| 国产成人久久激情91| 久久黄视频| 色欲久久久天天天综合网| 国产亚洲色婷婷久久99精品| 国产精品综合久久第一页| 欧美亚洲国产精品久久高清| 国产人久久人人人人爽| 精品乱码久久久久久夜夜嗨| 久久久久久久波多野结衣高潮| 狠狠色丁香久久婷婷综| 久久综合给合综合久久| 久久久久成人精品无码中文字幕 | 久久91亚洲人成电影网站| 日本加勒比久久精品| AV无码久久久久不卡蜜桃| 色诱久久av|