while() is faster than for() ???
#1

Hey everybody.
Just wanted to make sure that my tests are correct. When I'm running this code:
Код:
#include <a_samp>

main(){}

public OnGameModeInit( )
{
	new time, i = 0,
	tMax = 10000000;
	
	time = GetTickCount( );
	
	for( i=0; i < tMax; i++ ){}
	
	printf( "for loop = %i ms", GetTickCount( ) - time );
	
	time = 0, i = 0;
	
	time = GetTickCount( );
	while( i < tMax ){i++;}
	printf( "while loop = %i ms", GetTickCount( ) - time );
}
I get the following results:
Код:
[20:31:50] for loop = 648 ms
[20:31:50] while loop = 355 ms
Am I doing something wrong or while() loop is actually 2x faster ?? :O
Reply
#2

Dйjа vu. Do not worry about loops and how they're done, just use them efficiently!
Reply
#3

Interesting, though not reliable results:
Quote:

[19:50:56] for loop = 677 ms
[19:50:57] while loop = 1256 ms

Pcode for "for" loop:
pawn Код:
jump 2
l.0     ; a0
    ; line c
    break   ; a0
    load.s.pri fffffff8
    inc.s fffffff8
    ;$exp
l.2
    load.s.pri fffffff8
    push.pri
    load.s.pri fffffff4
    pop.alt
    xchg
    sless
    jzer 1
    ;$exp
    jump 0
l.1     ; e4
Pcode for "while" loop:
pawn Код:
break   ; 190
l.3     ; 194
    ; line 13
    break   ; 194
    load.s.pri fffffff8
    push.pri
    load.s.pri fffffff4
    pop.alt
    xchg
    sless
    jzer 4
    ;$exp
    ; line 13
    break   ; 1c0
    load.s.pri fffffff8
    inc.s fffffff8
    ;$exp
    jump 3
l.4     ; 1dc
Also 10000000 is quite a big number, the difference in real world is negligible, you won't have a 10^7 iterations ever.
Reply
#4

I can assure you that the speed comparison won't matter unless you're going to perform many iterations, by many I mean in more than thousands.
Reply
#5

I've run exactly your code, and this are the results i get:
Код:
[02:47:28] for loop = 511 ms
[02:47:29] while loop = 874 ms
So a for loop is still faster for me... :P
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)