哪个更快 - if..else或Select..case? [英] Which is faster - if..else or Select..case?
问题描述
我有三个条件比较。哪一个在下面两个之间更快?请指点我。谢谢大家!
I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all!
If var = 1 then
Command for updating database
ElseIf var = 2 then
Command for updating database
ElseIf var = 3 then
Command for updating database
EndIf
和
Select Case var
Case 1
Command for updating database
Case 2
Command for updating database
Case 3
Command for updating database
End Select
推荐答案
如果你编译这两个片段,并使用反射器反汇编,你会看到他们都结束了几乎相同的IL。编译程序用 if / else
替换 case
语句。
If you compile the two fragments and use reflector to disassemble you will see that they both end up as the practically the same IL. The compiler replaces the if / else
with case
statement.
如果你有性能问题,这种微优化是不太可能帮助你。
This kind of micro optimization is highly unlikely to help you if you have performance problems.
如果您遇到性能问题,则需要对程序进行配置,并找出瓶颈在哪里。
If you have performance problems then you need to profile the program and find out where the bottlenecks are.
如果您没有性能问题,请停止冒出这些东西,并担心编写容易理解的代码。
If you don't have performance problems, stop sweating this stuff and worry about writing code that is easily understood.
这篇关于哪个更快 - if..else或Select..case?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!