If Statements x &&
#1

Which is more faster:

PHP код:
if(&& b
or

PHP код:
if(a) if(b
Only respond if the tests and explanation. Debug ASM help a lot.
Reply
#2

I beleive the first statement, just because surelly it would be quicker for the computer to process if(a && b) instead of two different if statements?

+rep if I helped.
Reply
#3

Quote:
Originally Posted by iRemix
Посмотреть сообщение
I beleive the first statement, just because surelly it would be quicker for the computer to process if(a && b) instead of two different if statements?

+rep if I helped.
Hum, but, this makes the conditional "a" and conditional "b". Connected by a &&. So 3 conditional ?!. Make speed tests. Please
Reply
#4

&& is fastes.With loop 1 000 000 as 200ms ...
Reply
#5

Thank you.

Test it, please:

pawn Код:
#include a_samp

#define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
while(__d==GetTickCount());__c=GetTickCount();__d= __c;while(__c-__d<__a||\
__e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
GetTickCount());__c=GetTickCount();__d=__c;__b=0;} }{


#define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
%0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}


main()
{
    new f = 0;
    new t = 1;

    START_BENCH(1000);
    if(t && f) {}
    if(f && t) {}
    FINISH_BENCH("2");

    START_BENCH(1000);
    if(t) if(f) {}
    if(f) if(t) {}
    FINISH_BENCH("1");
   
}

/*
[16:11:55]  Bench for 2: executes, by average, 3445.79 times/ms.
[16:11:57]  Bench for 1: executes, by average, 4688.54 times/ms.
*/
Reply
#6

Код:
[16:14:40]  Bench for 2: executes, by average, 3473.32 times/ms.
[16:14:42]  Bench for 1: executes, by average, 3804.91 times/ms.
Small diference..


@Edit:
Other method.
pawn Код:
#include a_samp

public OnFilterScriptInit()
{


    new f = 0;
    new t = 1;

    new locate = GetTickCount();
   
    for(new i; i != 1000000; ++i)
    {
        if(t && f) {}
        if(f && t) {}
    }
   
    printf("Reply of metod whidth &&: %d", GetTickCount() - locate);

    locate = GetTickCount();
   
    for(new i; i != 1000000; ++i)
    {
        if(t) if(f) {}
        if(f) if(t) {}
    }
   
    printf("Reply of metod whidth IF: %i", GetTickCount() - locate);

}
Код:
[16:19:22] Reply of metod whidth &&: 92
[16:19:22] Reply of metod whidth IF: 94
Reply
#7

( a && b )
Код:
	proc	; main
	; line 2
	; line 3
	break	; c
	;$lcl a fffffffc
	push.c 0
	;$exp
	;$lcl b fffffff8
	push.c 0
	;$exp
	; line 4
	break	; 20
	load.s.pri fffffffc
	jzer 1
	load.s.pri fffffff8
	jzer 1
	const.pri 1
	jump 2
l.1
	zero.pri
l.2
	jzer 0
	;$exp
l.0		; 60
	stack 8
	zero.pri
	retn
if(a) if (b)
Код:
	proc	; main
	; line 2
	; line 3
	break	; c
	;$lcl a fffffffc
	push.c 0
	;$exp
	;$lcl b fffffff8
	push.c 0
	;$exp
	; line 4
	break	; 20
	load.s.pri fffffffc
	jzer 0
	;$exp
	; line 6
	break	; 34
	load.s.pri fffffff8
	jzer 1
	;$exp
l.1		; 48
l.0		; 48
	stack 8
	zero.pri
	retn
Take your own conclusions.
Reply
#8

In &&. If the "B" is false, it jumps to another spot in memory (with "A" true)

PHP код:
    LOAD.S.pri    fffffff8
    JZER          1
    LOAD
.S.pri    fffffffc
    JZER          1
    
CONST.pri     1
    JUMP          2
l.1
    ZERO
.pri
l.2
    JZER          0
                               
$exp
l.0                            
a8 
Without &&
PHP код:
    LOAD.S.pri    fffffff8
    JZER          3
                               
$exp
    LOAD
.S.pri    fffffffc
    JZER          4
                               
$exp
l.4                            
c8
l.3                            
c8 
Garfield:
The first code to be executed takes less. Try replacing the && down and see the results
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)