2

I've created a loop where a variable is used to test if the current run-through of the loop is the first one. Its fairly simple:

$firstrun = true;
while(condition){
  if($firstrun)
    // Do this
  else
    // Do that

  // Change $firstrun to false
}

I was just wondering (mostly out of curiosity because I'm it makes no real noticeable difference), when I need to change $firstrun to false, would be more efficient to test if the variable is true before assigning it to false or simply reassign it to false during each run-through?

Ex:

$firstrun = true;
while(condition){
  if($firstrun)
    // Do this
  else
    // Do that

  if($firstrun)
    $firstrun = false;
}

or simply

$firstrun = true;
while(condition){
  if($firstrun)
    // Do this
  else
    // Do that

  $firstrun = false;
}

PS: I guess this is a bad example also, because it would be most efficient to throw the reassignment of $firstrun in with the original condition, but as I said this is out of curiosity so I guess just pretend that is not an option for some reason.

PSS: I was coding in PHP when this idea hit me, but I'm guessing the solution would be language agnostic. Just thought I would throw that in there in case it does for some reason matter.

So ultimately, which is faster, condition testing or variable assignment?

Ben Kulbertis
  • 1,713
  • 4
  • 17
  • 30
  • 2
    There *may* be an absolute answer for PHP, but as a language agnostic question the only real answer is: profile it! – deceze Dec 30 '10 at 06:43

3 Answers3

4

none of the above

$firstrun = true;
while(condition)
{
  if($firstrun)
  {
    $firstrun = false;
  }
  else
  {
  }
}

reason I said so, because you are repetitively re-assign false to $firstrun, which you should just do at the first loop

condition test vs assignment which is faster?

for example you have shown, is the same (one execution cycle without some expensive call)

updated

I think condition testing will be slower, cause you might invoke series of subsequent action after that

ajreal
  • 46,720
  • 11
  • 89
  • 119
  • 2
    Not that the difference in speed will really matter. :) – Sasha Chedygov Dec 30 '10 at 06:57
  • Thanks ajreal. I suppose I should have just used my code exactly for the example. The real reason that this all came about is that the difference between my code is so small for that first run that I am actually using a ternary operator for the differences, and I like it there because it greatly improves readability in the code, but obviously I can't include a code block in a ternary as that would defeat its purpose, so I was going to do it at the end as shown above. But, as you said l, condition testing will be slower, and that's all I really need to know :) – Ben Kulbertis Dec 31 '10 at 14:55
1

This could be better, depending on what condition actually is:

if (condition) {

    //execute first run code

    while (condition) {
        //execute subsequent run code
    }
}

Given your example, you don't need the extra variable.

You don't even need the if statement if you know the code will always run at least once:

//execute first run code

while (condition) {
    //execute subsequent run code
}
thirtydot
  • 224,678
  • 48
  • 389
  • 349
0
<?php

class Test
{
    private $var = 156135135;
    const SOMETHING = 156135135;

    public function assign()
    {
        $this->var = self::SOMETHING;
    }

    public function conditionalAssign()
    {
        if ($this->var != self::SOMETHING) {
            $this->var = SELF::SOMETHING;
        }
    }

}

$obj = new Test;

$start = microtime(true);
for ($i = 1; $i < 10000000; ++$i) {
    $obj->assign();
}
echo round((microtime(true) - $start) * 1000, 2).' ms'.PHP_EOL;

$start = microtime(true);
for ($i = 1; $i < 10000000; ++$i) {
    $obj->conditionalAssign();
}
echo round((microtime(true) - $start) * 1000, 2).' ms'.PHP_EOL;

conditionalAssign always faster when variable is integer, often faster when variable is boolean and almost equal, when variable is string.