VB.NET 选择案例编译器优化?
VB.NET 2008 编译器是否有选择地优化 Select Case 语句?
例如,具有足够数量的整数事例的 Select Case 语句可以被组织为二分搜索。
我问这个问题是因为我很好奇是否应该选择 Select Case 来代替具有多个 Else If 的 If 语句,其中比较整数或其他基本数据类型。
Does the VB.NET 2008 compiler selectively optimize Select Case Statements?
For example, a Select Case Statement with a sufficient quantity of integer cases could be organized as a binary search.
I ask this because I am curious whether or not I should opt for a Select Case in place of If Statements with multiple Else If's where integers or other basic data types are being compared.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,除了这种性能微优化之外,您还应该担心代码的可读性和可维护性。
除非此开关位于执行 1000(数百万?)次的循环内,否则这不太可能成为应用程序的性能瓶颈。
做出决定并坚持下去,以保持一致性。一般来说,在分析了性能瓶颈所在之前,不要对代码进行性能调整。
另请参阅此问题。
In general, you should worry about code readability and maintainability over and above this sort of performance micro-optimisation.
Unless this switch is inside a loop which is being executed 1000's (millions?) of times, this is highly unlikely to be the performance bottlebeck of your app.
Make a decision and stick with it for consistency's sake. In general, don't performance tune code until you have analysed where your performance bottlenecks are.
See also this question.
具有 40 个选项的
Select Case
比包含 40 个ElseIf
语句的字符串快 10 倍以上。这比您期望通过二分搜索获得的改进要大。我猜想一个简单的整数Select Case
使用与计算的goto语句等效的现代机器代码——它进行编译,以便根据整数。我认为
Select Case
是合适的选择。Select Case
with 40 choices is more than 10x faster than a string of 40ElseIf
statements. That is more improvement than you would expect to get with a binary search. I would guess that a simple integerSelect Case
uses whatever the modern machine code equivalent of a computed goto statement is -- it compiles so that it branches directly to the proper "case" based on the value of the integer.I think
Select Case
is the one to go with.