DataRow[DataColumn] performance enhancements

Since the system I work on performs about 1000 DataTable specific manipulations per minute, I’ve been going through and working out some ways to speed it up that little bit more. Take the following code:

DataTable myData = new DataTable();
myData.ReadXml("myxml.xml");
if (myData.Rows[0].IsNull("column1"))
    return null;
else
    return myData.Rows[0]["column1"];

The obvious performance improvement here is to first pull out the column index from the DataTable and then also pull out the row you need. You would end up with something like this:

DataTable myData = new DataTable();
myData.ReadXml("myxml.xml");

DataRow row = myData.Rows[0];
int col = myData.Columns.IndexOf("column1");

if (col == -1)
    throw new Exception("The column 'column1' was not found.");

if (row.IsNull(col))
    return null;
else
    return row[col];

I first changed my code to work in this way but I was thinking to myself that the DataRow will still have to perform an index pull from its internal collection even when using the verified index for the column. Another way of doing this would be to pull out the DataColumn and use that in the place of the column index, but I had no idea what would be faster. So I fired up Reflector and took a look at the Int32 version of the index lookup:

public object this[int columnIndex]
{
    get
    {
        DataColumn column = this._columns[columnIndex];
        int defaultRecord = this.GetDefaultRecord();
        return column[defaultRecord];
    }
    set
    {
        DataColumn column = this._columns[columnIndex];
        this[column] = value;
    }
}

As you can see, internally the lookup finds the DataColumn first anyway, which means it would be faster for us to get the DataColumn in our own code first as we reference it twice. So our new code becomes:

DataTable myData = new DataTable();
myData.ReadXml("myxml.xml");

DataRow row = myData.Rows[0];
DataColumn col = myData.Columns["column1"];

if (col == null)
    throw new Exception("The column 'column1' was not found.");

if (row.IsNull(col))
    return null;
else
    return row[col];

This is a very tiny performance improvement, but every little helps when it’s used many times a second.

About the Author

Sleuth

Hello, I'm Dave! I'm a software developer, and I've lived in Nottingham since October 2005, and I tend to write my random thoughts down here in the hope that they might help or entertain someone in the future. If you have a comment about any of the posts you see on this site, please submit it, as you might also be helping someone!

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>