DataRow indexing performance (integer vs. string)

简介: SOURCE:  http://www.codeproject.com/KB/database/datarow_indexing.aspx Article: Introduction   Background When I was developing...

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

目录
相关文章
|
4月前
|
缓存 安全 Java
【揭秘】String vs StringBuilder vs StringBuffer:三大字符串类的秘密较量!你真的知道何时该用哪个吗?
【8月更文挑战第19天】探讨Java中`String`、`StringBuilder`与`StringBuffer`的区别及应用场景。`String`不可变,适合做哈希表键或多线程共享。`StringBuilder`支持动态修改字符串,适用于单线程环境以提高性能。`StringBuffer`与`StringBuilder`功能相似,但线程安全。示例代码展示各类型的基本用法。选择哪种类型取决于具体需求和性能考量。
62 0
|
7月前
|
Linux C++
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
67 0
(C++)VS下sizeof(string(““))与linux-g++下sizeof(string(““))大小区别及原因剖析
|
C语言 C++
在VS和g++下的string结构的区别
在VS和g++下的string结构的区别
|
存储 算法 编译器
【C++】list的使用和基本迭代器框架的实现 & vs和g++下string结构的说明
【C++】list的使用和基本迭代器框架的实现 & vs和g++下string结构的说明
|
Java C++
Java - Switch VS String 爬坑
Java - Switch VS String 爬坑
104 0
|
C++
vs项目发布时提示类似Default-Web.config Connection String”参数不能为 Null 或空的报错问题
参考地址: http://stackoverflow.com/questions/28996604/the-defaultconnection-web-config-connection-string-argument-cannot-be-null-or Create a Parameters.xml file in the Project root with the following content: 意思就是在项目的要目录下,创建一个 Parameters.xml 名称的文件,内容就是上面的xml。
1293 0
|
21天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
31 2
|
2月前
|
Java
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性
本文深入探讨了Java中方法参数的传递机制,包括值传递和引用传递的区别,以及String类对象的不可变性。通过详细讲解和示例代码,帮助读者理解参数传递的内部原理,并掌握在实际编程中正确处理参数传递的方法。关键词:Java, 方法参数传递, 值传递, 引用传递, String不可变性。
65 1
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性