Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#

DataRow Indexing Performance (Integer vs. String)

Rate me:
Please Sign up or sign in to vote.
4.04/5 (7 votes)
4 Feb 2007CPOL2 min read 57.3K   353   16   9
Integer and string indexing of System.Data.DataRow columns - head to head

Background

When I was developing an application for Pocket PC that used quite a large DataSet, I wondered where I could gain at least some bits of performance. I was filling some data to a tree view control, storing some data, doing selects from DataTable and getting the data from columns of DataRows. As you know, you can access the DataRow's columns by using either string indexing (dataRow["NAME"] or integer indexing dataRow[3]).

I thought I could test performance of both indexers - I just wanted to know if I could still use string indexers (they are more comfortable to work with) - or rework the code of this app to use integer indexing. So here comes the performance tester.

The Code

The approach to solve this problem was to create a large enough database (DataTable in my case), then in loops traverse the rows, and test the indexing itself. This is done by passing it as a parameter to a dummy function:

C#
dummy(dataRow["STR1"]));

The DataTable's structure is as follows:

Sample image

The columns are named by their data types, so STR1 is a string, INT1 an integer, DATE1 the DateTime, etc.

The program first generates user-entered number of rows, then n-times does integer indexing test and string indexing test. The indexing test m-times gets each row in table and calls dummy function on each column of that row.

There is a measuring - how long does it take to do the integer indexing test and the string indexing test.

In the end, the application writes out how much integer and string indexing take in total, and each one in average.

Example:

int indexing
total: 42171,875 ms
each: 421,71875 ms

string indexing
total: 149312,5 ms
each: 1493,125 ms 

Conclusion

According to the tests on my machine (Intel Core2 Duo 1,86 GHz, 1 GB RAM), integer indexing is approximately 3.5 - 4 times faster than the string indexing (results vary according to row count and repetition count).

Clearly, integer indexing is faster, but when we take in account that both indexers take a very short time (about 1 µs) to access a column, the difference is noticeable only when applied on large amounts of data.

History

  • 5th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer MicroStep
Slovakia Slovakia
Juraj Borza is currently working as a full-time C# developer located in Bratislava, Slovakia. He likes coding for fun and releases some projects under open-source license.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dr. Song Li27-Sep-11 7:48
Dr. Song Li27-Sep-11 7:48 
GeneralQuickest of all.... Pin
SimmoTech5-Feb-07 0:47
SimmoTech5-Feb-07 0:47 
GeneralRe: Quickest of all.... Pin
Juraj Borza5-Feb-07 1:26
Juraj Borza5-Feb-07 1:26 
GeneralRe: Quickest of all.... Pin
SimmoTech5-Feb-07 1:39
SimmoTech5-Feb-07 1:39 
GeneralRe: Quickest of all.... Pin
Juraj Borza5-Feb-07 3:34
Juraj Borza5-Feb-07 3:34 
GeneralRe: Quickest of all.... Pin
devnet2475-Feb-07 3:33
devnet2475-Feb-07 3:33 
GeneralRe: Quickest of all.... Pin
Juraj Borza5-Feb-07 3:38
Juraj Borza5-Feb-07 3:38 
GeneralRe: Quickest of all.... [modified] Pin
devnet2475-Feb-07 4:05
devnet2475-Feb-07 4:05 
Are you saying if I have 3 columns like
Name -Surname and street i can do

DataColumn columnName = dt.Columns["Name"];
DataColumn columnSurname = dt.Columns["Surname"];
DataColumn columnStreet = dt.Columns["Street"];

for (int i = 0; i < loops; i++)
{

foreach (DataRow dr in rows)
{
dummy(dr[columnName]);
dummy(dr[columnSurname]);
dummy(dr[columnStreet]);
}
}
=========================================
So far I have been doing as follows: How Can I speed it up

//Enterprise Library calling a SP
DataSet ds = db.ExecuteDataSet(dbCommand);
DataTable dt = ds.Tables[0];


for (int i = 0; (i < dt.Rows.Count); i = (i + 1))
{
if ((row["ID"] != DBNull.Value))
{
Customer.ID = ((string)(row["ID"]));
}

if ((row["Name"] != DBNull.Value))
{
Customer.Name= ((String)(row["Name"]));
}

etc....

}



Thanks.


-- modified at 10:18 Monday 5th February, 2007

thanks a lot

GeneralRe: Quickest of all.... Pin
Brian Leach12-Feb-07 12:04
Brian Leach12-Feb-07 12:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.