Ben Kulbertis
Ben Kulbertis

Reputation: 1713

Performance: condition testing vs assignment

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?

Upvotes: 2

Views: 827

Answers (3)

Karolis Mačiulskis
Karolis Mačiulskis

Reputation: 1

<?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.

Upvotes: 0

thirtydot
thirtydot

Reputation: 228302

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
}

Upvotes: 1

ajreal
ajreal

Reputation: 47331

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

Upvotes: 4

Related Questions