I was thinking - is there a much faster way to get results using continue state?
for ($i=0; $i<5000; $i++) {
if (!($i % 2)) {
continue;
}
do_something_odd($i);
}
Is faster ordinary loop with if/else or is faster skipping results using continue?
I am asking because of the best approach for performance and optimizaton.
well I can show you the op code differences
this is for
for ($i=0; $i<5000; $i++) { if (!($i % 2)) { continue; } do_something_odd($i); } function do_something_odd($number) { echo $number; }the opcode will be
function name: (null) number of ops: 23 compiled vars: !0 = $i line #* E I O op fetch ext return operan --------------------------------------------------------------------------------- 2 0 E > EXT_STMT 1 ASSIGN !0, 0 2 > JMP ->17 3 3 > EXT_STMT 4 MOD ~2 !0, 2 5 BOOL_NOT ~3 ~2 6 > JMPZ ~3, -> 4 7 > EXT_STMT 8 > JMP ->15 6 9 > EXT_STMT 10 INIT_FCALL_BY_NAME 'do_so 11 EXT_FCALL_BEGIN 12 SEND_VAR_EX !0 13 DO_FCALL 0 14 EXT_FCALL_END 2 15 > POST_INC ~5 !0 16 FREE ~5 17 > IS_SMALLER ~6 !0, 50 18 EXT_STMT 19 > JMPNZ ~6, -> 9 20 > EXT_STMT 21 NOP 11 22 > RETURN 1 branch: # 0; line: 2- 2; sop: 0; eop: 2; out1: 17 branch: # 3; line: 3- 3; sop: 3; eop: 6; out1: 7; out2: 9 branch: # 7; line: 4- 4; sop: 7; eop: 8; out1: 15 branch: # 9; line: 6- 2; sop: 9; eop: 14; out1: 15 branch: # 15; line: 2- 2; sop: 15; eop: 16; out1: 17 branch: # 17; line: 2- 2; sop: 17; eop: 19; out1: 20; out2: 3 branch: # 20; line: 9- 11; sop: 20; eop: 22; out1: -2 path #1: 0, 17, 20, path #2: 0, 17, 3, 7, 15, 17, 20, path #3: 0, 17, 3, 9, 15, 17, 20, Function do_something_odd: filename: /home/j/development/testing/php-opcode/string_concationation.php function name: do_something_odd number of ops: 6 compiled vars: !0 = $number line #* E I O op fetch ext return operan --------------------------------------------------------------------------------- 9 0 E > EXT_NOP 1 RECV !0 10 2 EXT_STMT 3 ECHO !0 11 4 EXT_STMT 5 > RETURN null branch: # 0; line: 9- 11; sop: 0; eop: 5; out1: -2for using just an if
<?php for ($i=0; $i<5000; $i++) { if (($i % 2)) { do_something_odd($i); } } function do_something_odd($number) { echo $number; }function name: (null) number of ops: 20 compiled vars: !0 = $i line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > EXT_STMT 1 ASSIGN !0, 0 2 > JMP ->14 3 3 > EXT_STMT 4 MOD ~2 !0, 2 5 > JMPZ ~2, ->12 4 6 > EXT_STMT 7 INIT_FCALL_BY_NAME 'do_something_odd' 8 EXT_FCALL_BEGIN 9 SEND_VAR_EX !0 10 DO_FCALL 0 11 EXT_FCALL_END 2 12 > POST_INC ~4 !0 13 FREE ~4 14 > IS_SMALLER ~5 !0, 5000 15 EXT_STMT 16 > JMPNZ ~5, ->3 9 17 > EXT_STMT 18 NOP 11 19 > RETURN 1 branch: # 0; line: 2- 2; sop: 0; eop: 2; out1: 14 branch: # 3; line: 3- 3; sop: 3; eop: 5; out1: 6; out2: 12 branch: # 6; line: 4- 2; sop: 6; eop: 11; out1: 12 branch: # 12; line: 2- 2; sop: 12; eop: 13; out1: 14 branch: # 14; line: 2- 2; sop: 14; eop: 16; out1: 17; out2: 3 branch: # 17; line: 9- 11; sop: 17; eop: 19; out1: -2 path #1: 0, 14, 17, path #2: 0, 14, 3, 6, 12, 14, 17, path #3: 0, 14, 3, 12, 14, 17, Function do_something_odd: filename: /home/j/development/testing/php-opcode/string_concationation.php function name: do_something_odd number of ops: 6 compiled vars: !0 = $number line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 9 0 E > EXT_NOP 1 RECV !0 10 2 EXT_STMT 3 ECHO !0 11 4 EXT_STMT 5 > RETURN null branch: # 0; line: 9- 11; sop: 0; eop: 5; out1: -2 path #1: 0, End of function do_something_oddthe thing is -> based on pure logic -> the one without the extra instruction could be faster -> so the answer as always -> it can! but in your case it probably wont .