Szybkość kodu z pętlą i kodu równoważnego bez niej
Ostatnio zmodyfikowano 2011-03-13 21:29
DejaVu Temat założony przez niniejszego użytkownika |
Szybkość kodu z pętlą i kodu równoważnego bez niej » 2011-03-13 14:07:46 W wyniku małej wojenki pt 'kto ma rację' na forum byłego gamedeva postanowiłem napisać program, udowadniający moją tezę, że każda zmienna w funkcji ma znaczenie dla wydajności programu. Efektem tego jest poniższy program:
#include <cstdio> #include <tools/Time.hpp>
const long COUNT = 100000000; const long REPEAT = 10;
struct RTime { double fSumTime; long long iSum; RTime() : fSumTime( 0 ) , iSum( 0 ) { } };
bool checkBit( long long iVal, long iBit ) { return(( iVal >> iBit ) & 0x1 ) > 0 ? true : false; }
long long useFor( long long iVal ) { for( size_t i = 0; i < 8; i++ ) if( !checkBit( iVal, i ) ) return 0; return 1; }
long long useIf( long long iVal ) { if( !checkBit( iVal, 0 ) ) return 0; if( !checkBit( iVal, 1 ) ) return 0; if( !checkBit( iVal, 2 ) ) return 0; if( !checkBit( iVal, 3 ) ) return 0; if( !checkBit( iVal, 4 ) ) return 0; if( !checkBit( iVal, 5 ) ) return 0; if( !checkBit( iVal, 6 ) ) return 0; if( !checkBit( iVal, 7 ) ) return 0; return 1; }
long long useIfVar( long long iVal ) { size_t i = 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; if( !checkBit( iVal, i++ ) ) return 0; return 1; }
long long test1( long iCount, RTime & time ) { tools::CTime timer; timer.Set( 0 ); long long iSuma = 0; for( long long i = 0; i < COUNT; i++ ) iSuma += useFor( i ); timer.Update(); time.fSumTime = timer.Get(); return iSuma; }
long long test2( long iCount, RTime & time ) { tools::CTime timer; timer.Set( 0 ); long long iSuma = 0; for( long long i = 0; i < COUNT; i++ ) iSuma += useIf( i ); timer.Update(); time.fSumTime = timer.Get(); return iSuma; }
long long test3( long iCount, RTime & time ) { tools::CTime timer; timer.Set( 0 ); long long iSuma = 0; for( long long i = 0; i < COUNT; i++ ) iSuma += useIfVar( i ); timer.Update(); time.fSumTime = timer.Get(); return iSuma; }
typedef long long( * FN_BenchmarkT )( long, RTime & );
long long benchmark( FN_BenchmarkT fn, const char * sName, long iCount ) { long long iResult; RTime score; printf( "====== BENCHMARK ======\n" ); printf( "Benchmark title: %s\n", sName ); tools::CTime timer; timer.Set( 0 ); for( volatile int i = 0; i < REPEAT; i++ ) iResult = fn( iCount, score ); timer.Update(); printf( "Sum time = %lf\n", score.fSumTime ); printf( "Total time = %lf\n", timer.Get() ); printf( "Result = %ld\n", iResult ); return iResult; }
int main() { benchmark( test1, "test1 - for", COUNT ); benchmark( test2, "test2 - if", COUNT ); benchmark( test3, "test2 - if + var", COUNT ); return 0; }
Czasy - Visual C++ 2008 - Release (maksymalna optymalizacja):
====== BENCHMARK ======
Benchmark title: test1 - for
Sum time = 1.828047
Total time = 18.426874
Result = 390625
====== BENCHMARK ======
Benchmark title: test2 - if
Sum time = 0.985514
Total time = 9.775179
Result = 390625
====== BENCHMARK ======
Benchmark title: test2 - if + var
Sum time = 1.049978
Total time = 10.450777
Result = 390625
|
|
McAffey |
» 2011-03-13 21:18:47 A możesz zapodać linkiem do samej wojenki ;] |
|
DejaVu Temat założony przez niniejszego użytkownika |
» 2011-03-13 21:29:35 |
|
« 1 » |