Is there any extra overhead for finding a column in a DataTable by name and not by index?

DataTableDoes the object have additional overhead for finding the column value by name thisRow("ColumnA"), not the column index thisRow(0)? In what scenarios can this be a problem.

I am working on a team that has a lot of experience writing VB6 code, and I noticed that I did not search for columns by name for objects DataTableor a data grid. Even in .NET code, we use a set of integer constants to refer to column names in these types of objects. I asked our team to explain why this is so, and he mentioned that there was a lot of overhead in VB6 for finding data by column name, not index. Is this still true for .NET?


Sample code (in VB.NET, but the same applies to C #):

Public Sub TestADOData()
Dim dt As New DataTable

'Set up the columns in the DataTable    '
dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Description", GetType(String)))

'Add some data to the data table    '
dt.Rows.Add(1, "Fred", "Pitcher")
dt.Rows.Add(3, "Hank", "Center Field")

'Method 1: By Column Name   '
For Each r As DataRow In dt.Rows
  Console.WriteLine( _
   "{0,-2} {1,-10} {2,-30}", r("ID"), r("Name"), r("Description"))
Next

Console.WriteLine()

'Method 2: By Column Name   '
For Each r As DataRow In dt.Rows
  Console.WriteLine("{0,-2} {1,-10} {2,-30}", r(0), r(1), r(2))
Next

End Sub

Is there a case where method 2 provides the performance advantage of method 1?

+3
source share
5 answers

Yes , there should be a little overhead associated with finding columns by name, not by index. I would not worry about this if you did not continue to search for the same column in the loop as in your code example. Because then a little overhead can accumulate to measurable service data, depending on the number of rows in the table.

- - DataColumn. :

Dim dt As DataTable = ...

Dim idColumn As DataColumn = dt.Columns("ID")
Dim nameColumn As DataColumn = dt.Columns("Name")
Dim descriptionColumn As DataColumn = dt.Columns("Description")

For Each r As DataRow In dt.Rows

    ' NB: lookup through a DataColumn object, not through a name, nor an index: '
    Dim id = r(idColumn)
    Dim name = r(nameColumn)
    Dim description = r(descriptionColumn)

    ...
Next

: ! , : , , , ( , ). DataColumn , , .

+7

  • ArrayList
  • , -.
  • , .

, , ?

( ) , , 1,000,000

  • - 13.3
  • - 109.11
  • - 109.24

, .

+2

, , ( , ).

, . DataTable , . , , .

- DataColumn, , , .

+1

- :

Dim table As DataTable = ...

Dim foo As int = table.Columns("Foo")

For Each row As DataRow In table.Rows

    Dim data = row(foo)

Next

, , . , , . , , .

+1

, . . , , . , ( dbgview), .

0

Source: https://habr.com/ru/post/1740972/


All Articles