性能:条件测试与分配
我创建了一个循环,其中使用变量来测试循环的当前运行是否是第一个。它相当简单:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我这么说的上述原因都不是
,因为您重复地将 false 重新分配给
$firstrun
,您应该在第一个循环中执行此操作条件测试与分配哪个更快? code>
例如您所展示的,是相同的(一个执行周期,没有一些昂贵的调用)
更新
我认为条件测试会更慢,因为您可能会在此之后调用一系列后续操作
none of the above
reason I said so, because you are repetitively re-assign false to
$firstrun
, which you should just do at the first loopcondition 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
这可能会更好,具体取决于
条件
实际是什么:根据您的示例,您不需要额外的变量。
如果您知道代码总是至少运行一次,您甚至不需要
if
语句:This could be better, depending on what
condition
actually is: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:当变量为整数时,条件分配总是更快,当变量为布尔值且几乎相等时,当变量为字符串时,条件分配通常更快。
conditionalAssign always faster when variable is integer, often faster when variable is boolean and almost equal, when variable is string.