2011-07-26 23 views
2

このコードを実行するより効率的な方法はありますか?ここで各ループでより効率的に

For Each row As DataRow In dt.Rows 
    Dim ts1 As String = row(0).ToString 
    For index As Integer = 1 To 9 
     Dim colName As String 
     colName = dt.Columns(index).ToString 
     For Each row2 As DataRow In dtAppAvail.Rows 
      Dim colName2 As String 
      Dim ts2 As String 
      colName2 = row2("Day").ToString.Substring(0, 3) & " " & CType(row2("Date"), Date).ToString("dd/MM") 
      ts2 = row2("Timeslot").ToString 
      If colName = colName2 AndAlso ts1 = ts2 Then 
       row(index) = row2("AppointmentsBooked") 
      End If 
     Next 
    Next        
Next 
+1

はそれを動作していますか?それは遅いですか?効率性を改善する理由がありますか? –

+2

時期尚早な最適化...最適化はすべての悪の根源です。 –

+0

これはループのために非常に多くのために遅いです - それを小さくしようとすると、それはよりコンパクトですか? – scouserider

答えて

1
For Each row As DataRow In dt.Rows 
      For index As Integer = 1 To 9 
       For Each row2 As DataRow In dtAppAvail.Rows 
        Dim colName2 As String = row2("Day").ToString.Substring(0, 3) & " " & CType(row2("Date"), Date).ToString("dd/MM") 
        If dt.Columns(index).ToString = colName2 AndAlso row(0).ToString = row2("Timeslot").ToString Then 
         row(index) = row2("AppointmentsBooked") 
        End If 
       Next 
      Next 
     Next 

はなら...それは私が=少ないメモリストレージを想定し、私はあなたが大きな違いに気づくでしょう疑う以下の変数を使用して、より凝縮されたバージョン... ある

0

あなたのDatatableは巨大なので、ループを並列にします。 .NET 4に付属のparallel.foreachを使用することをお勧めしましたが、.NET 2を指定して以来、自分で実装することができます。

"並列プログラミングのためのパターン" を確認してください:

http://www.microsoft.com/download/en/details.aspx?id=19222 
関連する問題