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

时间: 2024-09-09 17:40:29

DataRow indexing performance (integer vs. string)的相关文章

temp1 =Integer.parseInt(String.valueOf(cardId.charAt(len)));这句为什么不能运行,错误是未知数据愿?

问题描述 importjava.util.*;publicclassStringPractise{publicstaticStringstr(){Scannerinput=newScanner(System.in);System.out.println("请输入数字串:");StringnumStr=input.next();returnnumStr;}//身份证验证publicstaticStringidentityCardId(){inttemp1=0;StringcardId=s

Integer.parseInt(String s ,int radix)方法使用解释

方法parseInt(String s,int radix)的目的是输出一个十进制数,这个数字是"String s"但是我们要知道他是多少进制的,而方法中"int radix"参数正是来表达这个信息的.  比如:parseInt(1010,2) 意思就是:输出2进制数1010在十进制下的数. 更直白地说:parseInt(String s,int radix)就是求"int radix"进制数"String s"的十进制数是多

Integer.parseInt(String s)一个奇怪的问题

问题描述 最近做项目遇到一个很奇怪的问题,一路debug下去到现在都还没找到原因.因此发到CSDN寻求各位大牛们的专业解答!publicclassTest{publicstaticvoidmain(String[]args){Strings="٢٠١٤٠٦";System.out.println(Integer.parseInt(s));}}本来预期输出是抛异常但是发现运行结果却是运行环境是jdk1.6百思不得其解然后就debug踪下代码发现最终会调用到java.lang.Charac

请问VB6.0中的基本类型系统的定义情况是怎么样的?为什么string可以转换为integer

问题描述 请问VB6.0中的基本类型系统的定义情况是怎么样的?为什么string可以转换为integer 请问VB6.0中的基本类型系统的定义情况是怎么样的?为什么string可以转换为integer 解决方案 string转换为integer用的是cint或者 val 函数.

int 和 String 互相转换的多种方法 (转)

转换 5.1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); 2). int i = Integer.valueOf(my_str).intValue(); 注: 字串转成 Double, Float, Long 的方法大同小异. 5.2 如何将整数 int 转换成字串 String ? A. 有叁种方法:

Mybatis中执行String类型的自己拼写的sql,不执行配置文件中的sql

Mybatis中执行String类型的自己拼写的sql,不执行配置文件中的sql 在自己的dao类中继承SqlSessionDaoSupport类 /** * @author herman.xiong * @since 0.1 * @param <T>实体类 * @param <PK>主键类,必须实现Serializable接口 */ package com.dao; import java.io.Serializable; import org.apache.log4j.Logg

Integer.parseInt(&amp;quot;&amp;quot;) Integer.valueOf(&amp;quot;&amp;quot;)和new Integer(&amp;quot;&amp;quot;)之间的区别

把一个String转换成int有Integer.parseInt(""). Integer.valueOf("")和new Integer("")这么几种方式,它们之间有什么区别呢?我们可以分别看一下它们的源码 //Integer.parseInt("") public static int parseInt(String s) throws NumberFormatException { return parseInt(s,

String.valueOf() 方法的使用

1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下列几种 String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串 String.valueOf(char c) : 将 char 变量 c 转换成字符串 String.valueOf(char[] data) : 将 char 数组 data 转换成字符串

Object转为String的几种形式

在java项目的实际开发和应用中,常常需要用到将对象转为 String这一基本功能.本文将对常用的转换方法进行一个总结.常用的方法有Object.toString(),(String)要转换的对象,String.valueOf(Object)等.下面对这些方法一一进行分析. 方法1:采用 Object.toString()方法 请看下面的例子: Object object = getObject(); System.out.println(object.toString()); 在这种使用方法中