Column index vs column name

  • Thread starter Thread starter S. Daum
  • Start date Start date
S

S. Daum

When using an in memory DataTable or a DataReader has anyone tested if there
is a performance difference between using the column index vs the column
name to address specific columns in a row? In my application, there might be
a hundred columns with lots of somewhat random access to various columns -
performance will be important.

Thanks in advance for any insight.

Steve
 
Usually referring to any indexed item via its ordinal will be faster than
referring to the item via its key name.

Having said that, referring to an item by its ordinal can be dangerous if
the sequence changes, while the names usually stay constant.
 
S.Daum,

The speed sequence is from quickest to slowest.
Columname
Index
string

However in my idea it is not worth to take a lot of steps to get the
columname, what I have seen in some programs (as well in strongly typed
dataset, because there is the columname just a property). That takes time
too.

Cor
 
You could define variables like CustomerNameIndex,
CustomerPhoneIndex, etc., then search the columns
once and set those variables, and then use
Columns(CustomerNameIndex). That would be faster
than Columns("CustomerName"), and wouldn't have
the column order hardcoded.

Robin
 
Indexes are certainly faster. If you need a good solution then you should
store column indexes (from column names) right before doing the operation.
I.e.:

int someColumnIndex = table.Columns.IndexOf("Some Column");
....
 
Thanks for the answers, I'll proceed assuming the column index is the
fastest method. My next question is this:

I'll be adding an unpredictable number of columns to my data table. I will
never delete one once it is added (in one session). Can I rely on the
ColumnIndex staying the same for a given column once I add it to the
DataTable?

Thanks again for your insight.

Steve
 
Cor, I believe you are incorrect on this.

Referring to a column by its name is slower than referring to the column by
its index.
 
Scott is correct. Anytime you reference an object by a quoted string it's
slower as the address has to be resolved at runtime. However, Cor is also
right if he means that the Column Name is referencing a strongly typed
dataset. In this case the name is used to name a property of a class that
maps over the datatable.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
 
Scott and Bill,

To process an item in a datarow you need to know the datacolumn. That
information is not in the datarow. Therefore.

The String finds the index
The index finds the datacolumn in the table
If you can give the datacolumn direct than that is the quickest.

However the datacolumn is not always there and therefore is in my opinion
mostly the index much quicker, than trying to find that yourself.

Cor
 
The question is, however, is this lookup done at design time (as VS creates
a ST TableAdapter) or at runtime during the column (late) binding process?
One consumes some of the developer's time, the other consumes the user's
time.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
 
Back
Top