views:

52

answers:

1

What is the time complexity of accessing a column by its name in an instance of DataRow?


object Foo(DataRow row, string columnName)
{
    // What is the time complexity of the below line O(1) / O(n) / ?
    return row[columnName];
}
+2  A: 

I took a peek with Reflector and can confirm that the code in question has a time complexity of O(1). The actual data is stored in DataColumn instances using a plain old array indexed by record number...one array per column. The DataColumn is obtained from the name via a Hashtable and the record number is obtained directly from the DataRow instance. So you have a hash table lookup and an array lookup both of which are O(1).

Brian Gideon
Chapeau for your efforts and investigation! You made me to look in Reflector by myself and understand how it really works. Thanks a lot!
Boris Modylevsky