for( ) loop vs while( ) loop | While( ) loop is 143x faster?
#1

Hi guys,

I created a little test, to check which loop(I mean for( ) and while( )) is faster. The results are very strange! I want to know, why there is a very big difference.

pawn Код:
new
    var,
    time
;
time = GetTickCount( );
for( new i; i < 10000000; i++ )
{
    for( var = 0; var < 100; var++ ) // using for( ) loop
    {
        SendClientMessage( 0, -1, "" );
    }
}
printf( "result1: %d ms", GetTickCount( ) - time );
time = GetTickCount( );
for( new i; i < 10000000; i++ )
{
    while( var < 100 ) // using while( ) loop
    {
        SendClientMessage( 0, -1, "" );
        var++;
    }
}
printf( "result2: %d ms", GetTickCount( ) - time );
Here is the results:
Код:
[13:40:26] result1: 39798 ms
[13:40:26] result2: 277 ms
Thanks.
Reply
#2

Your test is flawed. Var will still be 99 when you while loop starts.
Reply
#3

Okay, I fixed it, but I'm still getting a very big difference(yes, I compiled the code)!

pawn Код:
new
    var,
    time
;
var = 0;
time = GetTickCount( );
for( new i; i < 10000000; i++ )
{
    for( var = 0; var < 100; var++ ) // using for( ) loop
    {
        SendClientMessage( 0, -1, "" );
    }
}
printf( "result1: %d ms", GetTickCount( ) - time );
var = 0; // fix
time = GetTickCount( );
for( new i; i < 10000000; i++ )
{
    while( var < 100 ) // using while( ) loop
    {
        SendClientMessage( 0, -1, "" );
        var++;
    }
}
printf( "result2: %d ms", GetTickCount( ) - time );
results:
Код:
[14:07:10] result1: 39201 ms
[14:07:10] result2: 285 ms
Reply
#4

Код:
for( new i; i < 10000000; i++ )
{
    var = 0;
    while( var < 100 ) // using while( ) loop
    {
        SendClientMessage( 0, -1, "" );
        var++;
    }
}
?
Reply
#5

Actually while loop is slower..

Код:
//RESULT
[04:48:15] result1: 40726 ms
[04:49:15] result2: 59403 ms
Fixed code

pawn Код:
new  var, time  ;
    time = GetTickCount( );
    for( new i=0; i < 10000000; i++ )
    {
        for( var = 0; var < 100; var++ ) // using for( ) loop
        {
            SendClientMessage( 0, -1, "" );
        }
    }
    printf( "result1: %d ms", GetTickCount( ) - time );
    time = GetTickCount( );
    new i = 0;
   
    while(i<10000000)
    {
        var = 0;
        while( var < 100 ) // using while( ) loop
        {
            SendClientMessage( 0, -1, "" );
            var++;
        }
        ++i;
    }
    printf( "result2: %d ms", GetTickCount( ) - time );
Reply
#6

Thanks, xxmitsu. What a mistake... Nevermind... Here is the correct results:
Код:
[14:18:30] result1: 39727 ms
[14:19:27] result2: 57889 ms
Reply
#7

pawn Код:
new time = GetTickCount();

for(new i; i < 100000000; i++)
{
}

printf("for loop == %d", GetTickCount()-time);

time = GetTickCount();

new i;
while(i < 100000000)
{
    ++i;
}
printf("while loop == %d", GetTickCount()-time);
Код:
[12:31:11] for loop == 13810
[12:31:26] while loop == 15167
for loop is 1357ms (one and a third seconds) faster over 100 million iterations.

No additional function calls to mess up speed.
Reply
#8

Okay, so why in another way, while loop is faster?

pawn Код:
new
    time,
    Float:vHealth,
    Float:vPos[ 3 ]
;
time = GetTickCount( );
for( new vehicleid; vehicleid < MAX_VEHICLES; vehicleid++ )
{
    if( GetVehicleModel( vehicleid ) == 0 )
    {
        GetVehiclePos( vehicleid, vPos[ 0 ], vPos[ 1 ], vPos[ 2 ] );
        for( new a; a < 500; a++ )
        {
            if( !IsValidFireID( a ) )
            {
                if( GetVehicleDistanceFromPoint( vehicleid, 1.0, 1.0, 1.0 ) < 4.0 )
                {
                    GetVehicleHealth( vehicleid, vHealth );
                    SetVehicleHealth( vehicleid, vHealth - 8.0 - random( 5 ) );
                }
            }
        }
    }
}
printf( "result1: %d ms", GetTickCount( ) - time );
new
    vehicleid,
    b
;
time = GetTickCount( );
while( vehicleid < MAX_VEHICLES )
{
    if( GetVehicleModel( vehicleid ) == 0 )
    {
        GetVehiclePos( vehicleid, vPos[ 0 ], vPos[ 1 ], vPos[ 2 ] );
        while( b < 500 )
        {
            if( !IsValidFireID( b ) )
            {
                if( GetVehicleDistanceFromPoint( vehicleid, 1.0, 1.0, 1.0 ) < 4.0 )
                {
                    GetVehicleHealth( vehicleid, vHealth );
                    SetVehicleHealth( vehicleid, vHealth - 8.0 - random( 5 ) );
                }
            }
            b++;
        }
    }
    vehicleid++;
}
printf( "result2: %d ms", GetTickCount( ) - time );
Results:
Код:
[14:32:00] result1: 638 ms
[14:32:00] result2: 1 ms
Reply
#9

i too really wanna know about this in Java Scripting while loop is a bit faster
i always wonder for its performance differnce with for loop
Reply
#10

It's very strange... In one test, while loop is a little bit slower, but in another test - it's 638x faster !
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)