Seasons.NET

ちょっとした技術ブログです

ダフのデバイス

id:hajimehoshiのところで紹介されていたアルゴが気になったのでベンチ。

最適化の影響を外すため、Debug版で、
FAST = 0.036
OLD = 0.038
ちなみに単純コピー(*dst++=*src++)だと、
最適化では、::memcpy呼ばれてしまいます。
(アセンブラでみてみたらそうなっていた)

あくまで私のCPU(Core2Duo 2.2GHz)でやったことなので、
他のCPU等ではどうかわかりません。
ご指摘があればコメント欄でどうぞ。

// LoopTest01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include <boost/timer.hpp>

const int CopyLength = 1000*1000;
BYTE pSrc[CopyLength] = {0};
BYTE pDst[CopyLength] = {0};

#define FAST_LOOP
//#define OLD_LOOP

int _tmain(int argc, _TCHAR* argv[])
{
    ::memset( pSrc , 10 , CopyLength );

    BYTE* dst = pDst;
    BYTE* src = pSrc;

    boost::timer t;
#if defined( OLD_LOOP )
    for( int i = 0;i < CopyLength;i++ )
    {
        *dst++ = rand();
    }
#endif
#if defined( FAST_LOOP )
    register int n = (CopyLength + 7)>>3;
    switch( CopyLength & 7 ){
        case 0: do { *(dst++) = rand();
        case 1: *(dst++) = rand();
        case 2: *(dst++) = rand();
        case 3: *(dst++) = rand();
        case 4: *(dst++) = rand();
        case 5: *(dst++) = rand();
        case 6: *(dst++) = rand();
        case 7: *(dst++) = rand();
        }while( --n > 0 );
    }
#endif
    printf( "time : %f\n" , t.elapsed() );

    return 0;
}