« タイマーAPIの精度は、どんなもん? | トップページ | タイマーをOS毎に切りかえるには! »

2005年8月13日 (土)

タイマーAPIの処理時間を計測すると…

前回の結果から
「QueryPerformanceCounter」が
最も正確なようだ。

桁違いの精度が、素晴らしい!

しかし処理時間は、どうだ?

あんまり処理時間が掛かり過ぎると
メインを圧迫するし…。

ということでこれらの処理に
どの程度の処理時間が
掛かるか調べてみた。

#pragma comment( lib, "WinMm.lib" )
//--------------------------------------
#include    <StdIo.h>
#include    <Windows.h>
//--------------------------------------
#define        COUNT_MAX    1000000
//--------------------------------------
double    Value;
DWORD    TimerData;
//--------------------------------------
//    比較用のダミータイマー関数
DWORD DummyTimer( void )
{
    return TimerData;
}
//--------------------------------------
void TimerCheck( void )
{
    LPSTR        Format    = "結果:%f (%s)\n";
    MMTIME        mmTime;
    int            i;
    LARGE_INTEGER    Start, End;
    LARGE_INTEGER    Frequency, PerformanceCount;
    DWORD            Count;

    QueryPerformanceCounter( &Start );
    for( i = 0 ; i < COUNT_MAX ; i++ )
        GetTickCount( );
    QueryPerformanceCounter( &End );
    printf( Format, ( End.QuadPart - Start.QuadPart ) / Value, "GetTickCount" );

    QueryPerformanceCounter( &Start );
    for( i = 0 ; i < COUNT_MAX ; i++ )
        timeGetTime( );
    QueryPerformanceCounter( &End );
    printf( Format, ( End.QuadPart - Start.QuadPart ) / Value, "timeGetTime" );

    QueryPerformanceCounter( &Start );
    for( i = 0 ; i < COUNT_MAX ; i++ )
        timeGetSystemTime( &mmTime, sizeof( mmTime ) );
    QueryPerformanceCounter( &End );
    printf( Format, ( End.QuadPart - Start.QuadPart ) / Value, "timeGetSystemTime" );

    QueryPerformanceFrequency( &Frequency );
    QueryPerformanceCounter( &Start );
    for( i = 0 ; i < COUNT_MAX ; i++ )
    {
        QueryPerformanceCounter( &PerformanceCount );
        Count    = ( DWORD )( PerformanceCount.QuadPart / Frequency.QuadPart );
    }
    QueryPerformanceCounter( &End );
    printf( Format, ( End.QuadPart - Start.QuadPart ) / Value, "QueryPerformanceCounter" );

    QueryPerformanceCounter( &Start );
    for( i = 0 ; i < COUNT_MAX ; i++ )
        DummyTimer( );
    QueryPerformanceCounter( &End );
    printf( Format, ( End.QuadPart - Start.QuadPart ) / Value, "DummyTimer" );
}
//--------------------------------------
void main( void )
{
#if 1    // 禁断の魔法
    HANDLE            Handle;
    Handle = OpenProcess( PROCESS_SET_INFORMATION, TRUE, GetCurrentProcessId( ) );
    SetPriorityClass( Handle, REALTIME_PRIORITY_CLASS );
    CloseHandle( Handle );
#endif

    LARGE_INTEGER    Frequency;

    QueryPerformanceFrequency( &Frequency );
    Value    = ( double )Frequency.QuadPart / 1000.0;

    // 何にもしないで実行すると…
    TimerCheck( );

    // でも、こうすると…
    // 何かに影響があるかもかも…
    timeBeginPeriod( 1 );
    TimerCheck( );
    timeEndPeriod( 1 );
}

前回、98と2Kでやったら
衝撃の結果が出たので
今回もやってみた…。

またもや衝撃の事実が!

次回!
「何でこんなに違う!98×2K」を
お送りします。

遊びは、置いといて…。

…。

参考までに実行した環境…。
PⅢ:1.3GHz:ソケット370

そこ『古!』言うなぁ!
うちじゃあ現役なんだから…。
ぷんすかぷん!

…。

さて、結果は…

98だと…

結果:  20.519955 (GetTickCount)
結果:3805.556580 (timeGetTime)
結果:3734.357767 (timeGetSystemTime)
結果:3429.589836 (QueryPerformanceCounter)
結果:   5.480313 (DummyTimer)

「timeBeginPeriod」を実行すると…

結果:  20.294507 (GetTickCount)
結果:3805.761914 (timeGetTime)
結果:3745.090431 (timeGetSystemTime)
結果:3439.929432 (QueryPerformanceCounter)
結果:   5.495399 (DummyTimer)

2Kだと…

結果:   7.154541 (GetTickCount)
結果:  73.100073 (timeGetTime)
結果: 125.352524 (timeGetSystemTime)
結果:1135.006265 (QueryPerformanceCounter)
結果:   5.460191 (DummyTimer)

「timeBeginPeriod」を実行すると…

結果:   6.998934 (GetTickCount)
結果:  73.548454 (timeGetTime)
結果: 125.933045 (timeGetSystemTime)
結果:1140.625135 (QueryPerformanceCounter)
結果:   5.470248 (DummyTimer)

「timeBeginPeriod」の影響は、
殆んど無いようだ…。

精度向上による処理時間の
変動は、関係無いと言えるかな…。

しかし、「GetTickCount」は
光ってんなぁ・・・。

ぶっちぎりの速さだ!

2Kに至っては、
ダミー関数に迫ってるし…!

まあヘルプにも「GetTickCount」は
メモリーを参照すると書いてあるので
多分最速?

結果から結局
「QueryPerformanceCounter」は、
桁違いに正確で桁違いに重い…。


まあ処理は、速いことに越したことはないので…

98の場合には、
「GetTickCount」と
「timeBeginPeriod」の

セットがお得。


2Kなら
「timeGetTime」と
「timeBeginPeriod」の

セットがお買い得だろう…。

備考…。

・その1
2Kの
「timeGetSystemTime」は、
「timeGetTime」を元にしているので
構造体に変換している分がネックになって
遅くなっているそうだ…。
(ヘルプから…。)、

・その2
98の
「timeGetTime」
「timeGetSystemTime」
「QueryPerformanceCounter」
は、ほぼ同じとは…。

多分…多分ですよ…
I/Oポートに律儀に
アクセスしているのでは?

I/Oアクセスは、重い…
つまりレスポンスが悪い…
要するに遅い!

と言う事だろうと思われる…。

…。

今回は、ここまで!

ああ、知恵熱が…。

|

« タイマーAPIの精度は、どんなもん? | トップページ | タイマーをOS毎に切りかえるには! »

コメント

コメントを書く



(ウェブ上には掲載しません)




トラックバック


この記事へのトラックバック一覧です: タイマーAPIの処理時間を計測すると…:

« タイマーAPIの精度は、どんなもん? | トップページ | タイマーをOS毎に切りかえるには! »