XML ファイル (リソース ファイル) から値を取得し、基本的にそれらをデータ テーブルに挿入しています。リソース ファイルから取得するキーは 679 個あり、これには 3.41 秒かかります。このループを高速化する方法があるかどうか疑問に思っていました。
Parallel.For ループを試してみましたが、前の挿入が完了していないときに行の挿入を開始するため、不安定であることがわかりました。同期ブロックを使用しましたが、速度が 3.41 に戻りました。
For idx As Integer = 0 To KeyNames.Length - 1
With KeyManagerResource.Instance
DataTableManager.Instance.InsertRow(KeyNames(idx), .GetKeyValue(KeyNames(idx), DynamicProperties.Instance.EnglishResourcePath), _
.GetKeyValue(KeyNames(idx), DynamicProperties.Instance.FrenchResourcePath))
End With
Next
''' <summary>
''' Gets the value of the key.
''' </summary>
''' <param name="ID">ID of the key.</param>
''' <returns>Value of the key.</returns>
''' <remarks></remarks>
Overrides Function GetKeyValue(ID As String, File As String) As String
'Sets the current path of the XMLReader to the english file.
XMLManager.Instance.SetReaderPath(File)
Dim returnedNode As Xml.XmlNode = XMLManager.Instance.GetNode(String.Format("//data" & Helper.CaseInsensitiveSearch("name"), "'" & ID.ToLower & "'"))
If returnedNode IsNot Nothing Then
Return returnedNode.ChildNodes(1).InnerText
Else
Return ""
End If
End Function
''' <summary>
''' Adds a row to the target table.
''' </summary>
''' <param name="RowValues">The row values we want to insert. These are in order, so it is presumed the first row value in the array is for the first column
''' of the target data table.</param>
''' <remarks></remarks>
Public Sub InsertRow(ByVal ParamArray RowValues() As String)
'If the length of the RowValues is not equal the columns, that means that is an invalid insert. Throw exception.
If RowValues.Length = dtTargetTable.Columns.Count Then
'Creates a new row.
Dim drNewRow As DataRow
drNewRow = dtTargetTable.NewRow
'Goes through the row values.
For idx As Integer = 0 To RowValues.Length - 1
'Store the value for the column.
drNewRow(dtTargetTable.Columns(idx)) = RowValues(idx)
Next
'Only adds the key if the primary key doesn't already exist.
If dtTargetTable.Rows.Find(RowValues(0)) Is Nothing Then
'Adds the row to the table.
dtTargetTable.Rows.InsertAt(drNewRow, 0)
End If
Else
Throw New Exception(String.Format("Invalid insert. The number of row values passed are not equal to the number of columns of the target dataTable." & _
"The number of columns of the target dataTable are {0}.", dtTargetTable.Columns.Count))
End If
End Sub