none
Performance issue when calling DataTable.Item(RowIndex) vs DataTable.Rows.Item(RowIndex) RRS feed

  • Question

  • I was wondering if anyone has noticed a huge performance issue when calling DataTable.Item(RowIndex) instead of DataTable.Rows.Item(RowIndex) in .NET 2.0?

    For some reason, I think functionality has changed for .NET 3.5, because DataTable.Item(RowIndex) uses Linq to find a DataRow, rather than just checking to see if the index is out-of-bounds and returning a default value.

    https://connect.microsoft.com/VisualStudio/feedback/details/674201/performance-issue-with-datatable-item-vs-datatable-rows-item-system-linq-enumerable-elementatordefault-shouldnt-be-used

     

    Steps to reproduce:

    Dim table As TypedTableBase(Of DataRow) = New DataTable()
    
    For index = 0 To 100
    table.Columns.Add(New DataColumn(index.ToString()))
    Next
    
    For index = 0 To 100
    table.Rows.Add(New table.NewRow())
    Next
    
    Dim Time As DateTime = DataTime.Now
    
    ' This is quick, so lets test this first. Should be under 1 minute.
    For Index As Integer = 0 To 300000
    'Correct:
    Dim Row as DataRow = Table.Rows(Index MOD Table.Rows.Count)
    Next
    
    ' Use breakpoint to see time elapsed. Should be less than one minute.
    Dim TimeElapsed As TimeSpan = Time.Subtract(DatTime.Now)
    
    Dim Time As DataTime = DataTime.Now
    
    For Index As Integer = 0 To 300000
    'Performance Warning!! : access to dataRow by ElementAtOrDefault
    'After 3 hours, set breakpoint and move cursor out of loop.
    ' It's performance is already too slow, and no need to waste time past then.
    Dim Row as DataRow = Table(Index MOD Table.Rows.Count)
    Next
    
    ' Use breakpoint to see time elapsed.
    TimeElapsed = Time.Subtract(DatTime.Now)
    

     

    Sunday, June 5, 2011 6:33 PM

Answers