Jump to content

php optimization; 1 or 2 if's


fatkatie

Recommended Posts

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

     }

}

 

Link to comment
Share on other sites

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 ?

 

 
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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
	}	
}

 

Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

  • 4 weeks later...

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";





 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.