Column index vs column name

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
 
S

Scott M.

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.
 
C

Cor Ligthert [MVP]

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
 
R

RobinS

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
 
M

Miha Markic [MVP C#]

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");
....
 
S

S. Daum

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
 
S

Scott M.

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.
 
W

William \(Bill\) Vaughn

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)
 
C

Cor Ligthert [MVP]

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
 
W

William \(Bill\) Vaughn

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top