VB.NET如何提供最佳性能“选择案例”或者IF ... ELSEIF ... ELSE ... END IF [英] VB.NET How give best performance "Select case" or IF... ELSEIF ... ELSE... END IF

查看:127
本文介绍了VB.NET如何提供最佳性能“选择案例”或者IF ... ELSEIF ... ELSE ... END IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的DataTable,我需要遍历每一行并验证一个特定的值。

I have a huge DataTable, and I need go by each row and validate an specific value.

哪种方法给我更多的性能,IF ELSE的结构或选择案例?
(我专注于提供最佳性能的方法)

Which method give me more performance, an structure of IF ELSE or SELECT CASE? (I'm focused in the method that offer me the best performance)

IF ELSE(METHOD#1)

IF ELSE (METHOD #1)

For Each vRow In vDTtemp.Rows
    If vRow("Item") = "Time" Then
        vRow("Result") = "000"
    ElseIf vRow("Item") = "DateTime" Then
        vRow("Result") = "001"
    ElseIf vRow("Item") = "String" Then
        vRow("Result") = "002"
    Else
        vRow("Result") = "N/A"
    End If
Next

SELECT CASE(METHOD#2)

SELECT CASE (METHOD #2)

For Each vRow In vDTtemp.Rows
    Select Case vRow("Item")
        Case "Time"
            vRow("Result") = "000"
        Case "DateTime"
            vRow("Result") = "001"
        Case "String"
            vRow("Result") = "002"
        Case Else
            vRow("Result") = "N/A"
    End Select
Next


推荐答案

我是sp在过去的几天里,我们花了很多时间研究这个问题并找到了一种比其他方法快得多的方法。我也发现在字符串变量上使用Select Case相当于一系列If / Else If语句,并且两者都令人失望地慢。

I've spent quite a lot of time working on this same problem over the last couple of days and have found one approach which is much faster than the others. I too found that using Select Case on a string variable was equivalent to a series of If/Else If statements, and both were disappointingly slow.

但是以下技术有效非常好,并将时间减少了50%以上。而不是原始代码:

However the following technique has worked very well, and reduced the amount of time by over 50%. Instead of the original code:

For Each vRow In vDTtemp.Rows
    Select Case vRow("Item")
        Case "Time"
            vRow("Result") = "000"
        Case "DateTime"
            vRow("Result") = "001"
        Case "String"
            vRow("Result") = "002"
        Case Else
            vRow("Result") = "N/A"
    End Select
Next

更改它以打开一个简单的布尔值,并使用String.Equals方法,如下所示:

Change it around to switch on a simple Boolean, and use the String.Equals method, like this:

For Each vRow In vDTtemp.Rows
    'Read out the row value so we only need to access the datarow once
    rowValue = vRow("Item")
    'Which of these statements is true?
    Select Case True
        Case rowValue.Equals("Time")
            vRow("Result") = "000"
        Case rowValue.Equals("DateTime")
            vRow("Result") = "001"
        Case rowValue.Equals("String")
            vRow("Result") = "002"
        Case Else
            vRow("Result") = "N/A"
    End Select
Next

通过以这种方式接近它,我得到了显着的改进,在一种情况下,将我的代码从100,000秒迭代循环中的1.3秒减少到0.5秒。如果这是一个经常被称为时间关键的代码段,那可能会产生很大的不同。

I've had significant improvements by approaching it in this way, in one case reducing my code from 1.3 seconds over a 100,000 iteration loop to 0.5 seconds. If this is in a really frequently-called time-critical section of code, that can make a big difference.

正如下面的评论所指出的那样,字符串的序数比较,如果使用非英语语言环境,可能不会导致预期的行为(请参阅示例的注释)。

As pointed out in the comments below however, this performs an "Ordinal" comparison of strings, which may not result in the expected behaviour if non-English locales are being used (see the comments for examples).

Adam。

这篇关于VB.NET如何提供最佳性能“选择案例”或者IF ... ELSEIF ... ELSE ... END IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆