DataRow indexing performance (integer vs. string)

SOURCE:  http://www.codeproject.com/KB/database/datarow_indexing.aspx

Article:

Introduction

 

Background

When I was developing an application for Pocket PC that used quite 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 else, 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 to 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 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: dummy(dataRow["STR1"]));.

The DataTable's structure is as follows:

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 did take integer and string indexing 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) the 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.

i will translate this article.while datacolumn is not contained.so add datacolumn test.

result:

How many rows in testing DataTable? 100000
How many times to process all rows in one loop? 100
How many times to repeat int/string indexing? 10
generating data...
------------------------ 0 ---------------------------
1.int indexing:   23583.912ms
2.string indexing:   39206.376 ms
3.DataColumn indexing:   20148.9728ms
------------------------ 1 ---------------------------
1.int indexing:   22842.8464ms
2.string indexing:   38715.6704 ms
3.DataColumn indexing:   20599.6208ms
------------------------ 2 ---------------------------
1.int indexing:   23393.6384ms
2.string indexing:   39586.9232 ms
3.DataColumn indexing:   21080.312ms
------------------------ 3 ---------------------------
1.int indexing:   21540.9744ms
2.string indexing:   36011.7824 ms
3.DataColumn indexing:   19918.6416ms
------------------------ 4 ---------------------------
1.int indexing:   24665.4672ms
2.string indexing:   36642.6896 ms
3.DataColumn indexing:   18947.2448ms
------------------------ 5 ---------------------------
1.int indexing:   21110.3552ms
2.string indexing:   38275.0368 ms
3.DataColumn indexing:   20479.448ms
------------------------ 6 ---------------------------
1.int indexing:   22332.112ms
2.string indexing:   39476.7648 ms
3.DataColumn indexing:   20329.232ms
------------------------ 7 ---------------------------
1.int indexing:   23964.4592ms
2.string indexing:   38885.9152 ms
3.DataColumn indexing:   21671.1616ms
------------------------ 8 ---------------------------
1.int indexing:   23163.3072ms
2.string indexing:   39877.3408 ms
3.DataColumn indexing:   21140.3984ms
------------------------ 9 ---------------------------
1.int indexing:   22862.8752ms
2.string indexing:   39636.9952 ms
3.DataColumn indexing:   21230.528ms
#####################################################
1# INT indexing
 total: 229459.9472 ms
 each: 22945.99472 ms
2# STRING indexing
 total: 386315.4944 ms
 each: 38631.54944 ms
3# DataColumn indexing
 total: 205545.56 ms
 each: 20554.556 ms

原文地址:https://www.cnblogs.com/AloneSword/p/2237537.html