首页 > 开发 > 综合 > 正文

数据库访问与本地文件访问实测报告

2024-07-21 02:25:10
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,一直认为数据库访问比本地文件访问是要慢很多的,但究竟慢多少却不清楚。终于忍不住作了下比较测试。

测试环境:os---windows 2000 server databaseserver----sqlserver 2000 程序语言:csharp

测试方式:从数据库中读取参数表/从xml中读取配置参数等同数量的记录并实例化对象。

测试程序如下:

using system;
using system.globalization;
using system.io;
using system.xml.serialization;
using system.xml;
using system.data;
using system.data.sqlclient;

public class demo
{
public static void main()
{
system.console.writeline("===== executexml() =====================================");
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline(executexml());
system.console.writeline("========================================================");

system.console.writeline("===== executesql() =====================================");
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline(executesql());
system.console.writeline("========================================================/n");

}

static double executesql()
{
string _connstring = "server=data-server;user id=sa;password=1234;database=leaddb;min pool size=4;max pool size=4;packet size=3072";
sqlcommand cmd = new sqlcommand();
sqlconnection conn = new sqlconnection(_connstring);

double begin = cultureinfo.invariantculture.calendar.getmilliseconds(datetime.now);
try {
conn.open();

cmd.connection = conn;
cmd.commandtext = "select [categoryid], [name] from [sys_category] where [categorytype] = 2";

cmd.commandtype = commandtype.text;
sqldatareader rdr = cmd.executereader(commandbehavior.closeconnection);
while (rdr.read())
{
new entity(rdr.getint32(0), rdr.getstring(1));
}
rdr.close();
}catch {
conn.close();
throw;
}
begin = cultureinfo.invariantculture.calendar.getmilliseconds(datetime.now) - begin;
return (begin >= 0)?begin:begin+1000.0;
}

static double executexml()
{
double begin = cultureinfo.invariantculture.calendar.getmilliseconds(datetime.now);
xmldocument xmldoc = new xmldocument();
xmldoc.load( @"f:/leadbridge/xcerp/web/onlineservice/sys_category_headship.xml" );

foreach(xmlnode node in xmldoc.selectnodes("items/item")){
new entity(int.parse(node.attributes["id"].value), node.innertext);
}
begin = cultureinfo.invariantculture.calendar.getmilliseconds(datetime.now) - begin;
return (begin >= 0)?begin:begin+1000.0;
}
}

public class entity {
public entity(int age, string name){
this._age = age;
this._name = name;
}
private int _age;
private string _name;
public int age{
get{ return _age; }
set{ _age = value; }
}
public string name{
get{ return _name; }
set{ _name = value; }
}
}

执行结果如下:

===== executexml() =====================================
47
0
0
16
0
0
0
0
16
0
========================================================
===== executesql() =====================================
203
0
0
0
0
0
16
0
0
0
========================================================

做过多次测试,基本上是如上数量级别(一个数量级)的差异(第一次执行差异),因此建议,大家对于无需经常修改的数据保存为本地参数文件形式比直接从数库读取效率要高一些。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表