English 中文(简体)
正在评估if…else if…块上的每个条件语句
原标题:Evaluating every conditional statement on an if... else if... block

Objective-C是否评估<code>上的每个语句,如果。。。否则,如果…块,或者它会在每个块到达时对其进行求值,然后在找到真实条件时跳过剩余的求值?

这更像是一个与绩效相关的务实问题,而不是其他任何问题。

是的,我确实知道if块本身的内容没有被执行,但我指的是被评估的实际语句。

实例

if ([condition A] == test) {
  // Do something
} else if ([condition B] == test) {
  // Do something    
} else if ([condition C] == test) {
  // Do something    
} else {
  // Do something because all other tests failed
}    

那么…如果条件A是真的,那么条件B和C无论如何都会得到评估吗?

如果他们这样做了,那么使用switch语句的执行方式是否相同,或者switch是否只测试每个条件,然后因为中断而退出评估?

我的理解是,在<code>上,如果。。。否则,if…块,每个条件都会被求值,因此使用开关或嵌套if s(ugh-不喜欢这里的想法)可能会更快地对大量数据进行大型求值操作(数十万项将与可能的一百条语句进行检查)。

只是好奇:-)

最佳回答

否,如果满足条件A,则不评估B和C。事实上,它们是其他条款的一部分,无论如何都不会被执行。

附带说明:if(condA||condB)或if(condA&;&;condB)也会延迟求值,即在第一种情况下,condB仅在condA为false时求值,在第二种情况下当condA为true时求值。

问题回答

它只会对他们进行评估。一个<code>如果。。。else if块等效于if。。else{if…}。它的行为与嵌套的if相同,只是格式很好,省略了一些大括号。

在[]的objective-c之外,其行为方式与c完全相同。

对于if()。。。否则如果()。。。其他链,这意味着每个表达式都要求值,直到其中一个求值为true并输入块为止。大体上

if (a) {
    ...
} else if (b) {
    ...
} else {
    ...
}

被解释为

if (a) {
    ...
} else {
    if (b) {
        ...
    } else {
        ...
    }
}




相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签