性能:条件测试与分配

发布于 2024-10-09 13:11:54 字数 880 浏览 0 评论 0原文

我创建了一个循环,其中使用变量来测试循环的当前运行是否是第一个。它相当简单:

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

  // Change $firstrun to false
}

我只是想知道(主要是出于好奇,因为它没有真正明显的区别),当我需要将 $firstrun 更改为 false 时,测试是否会更有效变量在将其分配为 false 之前为 true ,还是在每次运行期间将其重新分配为 false ?

例如:

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

  if($firstrun)
    $firstrun = false;
}

或简单地

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

  $firstrun = false;
}

PS: 我想这也是一个坏例子,因为将 $firstrun 的重新分配与原始条件一起放入是最有效的,但正如我所说,这是出于好奇,所以我想只是假装由于某种原因不是一个选择。

附: 当我想到这个想法时,我正在使用 PHP 进行编码,但我猜测该解决方案与语言无关。只是想我会把它扔在那里以防万一它因某种原因而重要。

那么最终,条件测试和变量赋值哪个更快?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-10-16 13:11:54

我这么说的上述原因都不是

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

,因为您重复地将 false 重新分配给 $firstrun,您应该在第一个循环中执行此操作

条件测试与分配哪个更快? code>

例如您所展示的,是相同的(一个执行周期,没有一些昂贵的调用)

更新

我认为条件测试会更慢,因为您可能会在此之后调用一系列后续操作

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

眼眸 2024-10-16 13:11:54

这可能会更好,具体取决于条件实际是什么:

if (condition) {

    //execute first run code

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

根据您的示例,您不需要额外的变量。

如果您知道代码总是至少运行一次,您甚至不需要 if 语句:

//execute first run code

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

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
}
痴者 2024-10-16 13:11:54
<?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;

当变量为整数时,条件分配总是更快,当变量为布尔值且几乎相等时,当变量为字符串时,条件分配通常更快。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文