Performance: condition testing vs assignment

拜拜、爱过 提交于 2019-12-10 10:49:34

问题


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?


回答1:


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




回答2:


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
}



回答3:


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



来源:https://stackoverflow.com/questions/4560570/performance-condition-testing-vs-assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!