fatkatie Posted July 19, 2018 Share Posted July 19, 2018 This is simple curiosity. I'd like to know if someone really knows the answer. I've always been suspicious of performance testing with loops. I have a loop in php. Within that loop I act based on two values, let's call them MOSTLY_FALSE and ALWAYS_CHANGING. Given MOSTLY_FALSE is seldom true, is the second "double" IF faster than the 'single', or will the short-circuit take care of it? I sometimes code with the double form just to let people know that truth is exceptional. Thanks. (Just curious.) if ( MOSTLY_FALSE == true && ALWAYS_CHANGING == 1) { // do this exceptional thing } if ( MOSTLY_FALSE == true) { // access ALWAYS_CHANGING for the first time (is a field returned in sql query if (ALWAYS_CHANGING == 1) { // do this exceptional thing } } Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/ Share on other sites More sharing options...
benanamen Posted July 19, 2018 Share Posted July 19, 2018 (edited) You fail to understand the if control structure. if is a Boolean check, therefore adding == true or == 1 is redundant. The if already determines true/false. There is no need to nest the statements. What you are asking in code is if A AND B is true, do whatever. KISS <?php if ($a && $b) echo "a and b are true"; ?> Feel free to RTFM. It's Free. http://php.net/manual/en/control-structures.if.php ? Edited July 19, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559843 Share on other sites More sharing options...
Barand Posted July 19, 2018 Share Posted July 19, 2018 The best way to get get an answer to that kind of question is to run timing tests on the options, $t1 - microtime(1); loop if ( MOSTLY_FALSE == true && ALWAYS_CHANGING == 1) { // do this exceptional thing } endloop $t2 = microtime(1); output ($t2 - $t1); Repeat for all options and see which is faster. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559844 Share on other sites More sharing options...
fatkatie Posted July 20, 2018 Author Share Posted July 20, 2018 barand - yah, I think you're correct. Timing the loop may be the best way. There are just too many things going on. I imagine the second assignment might change according to optimization rules - of which I have no clue. I'll try a few later. My bet? These will run at different speeds. We'll see. But ... Will microtime return the time spent actually executing? What if there is a process swap? For others, I should have stated the changing value was an integer. I've always wondered about the word 'redundant' as it relates to verbose. (Does RTFM mean what I think it does? ) Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559848 Share on other sites More sharing options...
benanamen Posted July 20, 2018 Share Posted July 20, 2018 (edited) Verbose means more words than necessary, redundant is saying the same exact thing more than once. RTFM means Read the Free Manual. Basically it is like your code stutters and repeats the exact same thing. Example: if true = true And true = true { //do something } Or if true = true { if true = true { //do something } } Edited July 20, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559849 Share on other sites More sharing options...
fatkatie Posted July 20, 2018 Author Share Posted July 20, 2018 barand - I think what I want is getrusage - wall clock time v/s cpu time. https://stackoverflow.com/questions/535020/tracking-the-script-execution-time-in-php Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559851 Share on other sites More sharing options...
benanamen Posted July 20, 2018 Share Posted July 20, 2018 (edited) Doing some quick tests, the multiple nested control structures take up to nine times longer to execute that a single control structure. It only makes sense that a single one is going to be faster than a double nested one. The clear winner if ($MOSTLY_FALSE && $ALWAYS_CHANGING) { // do this exceptional thing } Edited July 20, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1559854 Share on other sites More sharing options...
fatkatie Posted August 14, 2018 Author Share Posted August 14, 2018 Finally got around to this question of mine. Using the original parameters of mostly false I ran the following code. There may be many reasons why this test is not valid. I have no advanced knowledge concerning such profiling. I tried to keep the paths equal and to wash out any time glitch issues. It was found that the nested if construct was faster - every time - than the single line short circuit. It's probably due (I'd guess) to the need to access/build the second ALWAYS_CHANGING variable in the if construct. Env: PHP 5.6+ on Win7Pro64, executed as php script in command window. <?php $micro_time_param = true; $number_of_samples = 100000; $frequency_false = 99; $a_true_and_false = array_fill(0, $number_of_samples, false); $number_of_trues = $number_of_samples - intval($number_of_samples * $frequency_false / 100); $a_random_int_stuff = array(); // always true for ($idx = 0; $idx < $number_of_samples; $idx++) { $a_random_int_stuff[$idx] = rand(1, $number_of_samples); } // Scatter the true hits within the array $count_trues = $number_of_trues; while($count_trues--) { $r_index = rand(0, $number_of_samples - 1); $a_true_and_false[$r_index] = true; } $hit_1_count = 0; $hit_2_count = 0; $hit_1_sum = 0.0; $hit_2_sum = 0.0; $hit_1_start = ''; $hit_1_end = ''; $hit_2_start = ''; $hit_2_end = ''; $wash_loop = 1000; $dec_something = $wash_loop; while ($wash_loop--) { // LOOP 1 $count_samples = $number_of_samples; $hit_1_start = microtime($micro_time_param); while($count_samples--) { if ($a_true_and_false[$count_samples] && $a_random_int_stuff[$count_samples]) { $hit_1_count++; } } $hit_1_end = microtime($micro_time_param); $hit_1_sum += (float)($hit_1_end - $hit_1_start); // LOOP 2 $dec_something--; $count_samples = $number_of_samples; $hit_2_start = microtime($micro_time_param); while($count_samples--) { if ($a_true_and_false[$count_samples]) { if ($a_random_int_stuff[$count_samples]) { $hit_2_count++; } } } $hit_2_end = microtime($micro_time_param); $hit_2_sum += (float)($hit_2_end - $hit_2_start); } echo "Time hit 1 $hit_1_sum , hit count $hit_1_count\n"; echo "Time hit 2 $hit_2_sum , hit count $hit_2_count\n"; Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1560330 Share on other sites More sharing options...
fatkatie Posted August 14, 2018 Author Share Posted August 14, 2018 I just replaced the single line if using the second index array with a single value. It's still slower. $val = 22; while($count_samples--) { //if ($a_true_and_false[$count_samples] && $a_random_int_stuff[$count_samples]) { if ($a_true_and_false[$count_samples] && $val) { $hit_1_count++; } } Done. Out. Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1560331 Share on other sites More sharing options...
benanamen Posted August 14, 2018 Share Posted August 14, 2018 WTF??? I will go with what you said.... Quote "There may be many reasons why this test isnot valid. I have no advanced knowledge concerning such profiling." Quote Link to comment https://forums.phpfreaks.com/topic/307528-php-optimization-1-or-2-ifs/#findComment-1560332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.