分享
 
 
 

Inside MSXML Performance(MSXML性能分析) (4)

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

First DOM Walk Working Set Delta

第一次DOM树遍历引起的工作空间增量

Walking the DOM tree for the first time has an impact on the working set metric because some nodes in the tree are created on demand. To illustrate this, I'll show the working set deltas resulting from the first walk over the freshly loaded sample data:

在第一次遍历DOM树时,对工作空间这个度量指标会有一定的影响,因为树中有些节点会被创建。为了说明这一点,这里提供了首次遍历载入内存的样品数据时工作空间的增量:

Sample

样品

Working set delta (percentage)

工作空间增量(百分比)

Ado.xml

0

Hamlet.xml

25

Ot.xml

14

Northwind.xml

36

According to these results, it seems that the ADO attribute case doesn't have any nodes created on demand.

以上结果显示,ADO.xml在此时并不需要创建任何节点。

createNode Overhead

createNode的开销

Creating a DOM tree from scratch results in a higher peak working set than loading the same document from disk. To illustrate this, I created a DOM document with 10,000 elements looking like this:

在内存中临时创建一个DOM树比从磁盘上载入同样的文档会产生更高的工作空间峰值。为了说明这一点,这里创建了一个DOM文档,包含10,000个相同的如下元素:

<item>this is a test</item>

Then I compared the load time, load plus walk time (to force on-demand construction), and create time, showing associated working sets.

然后,比较载入时间,载入加遍历时间(强制构造)和直接创建时间,下表显示了相关的工作空间大小:

Function

功能

Time (milliseconds)

时间(单位为毫秒)

Working set (bytes)

工作空间(单位为字节)

Load

146

842,137

Load+Walk

148

1,173,914

Create

740

2,503,066

These results show that loading a document is roughly five times faster than creating the same document from scratch in memory. The reason is that the process of creating a document requires a lot of DOM calls, which slows things down. Merely loading the document bypasses all this and goes directly to the internal data structures.

以上结果显示载入一个文档比在内存中直接创建同样的文档要快差不多5倍。原因是创建文档时需要有多次DOM调用,执行就会变慢。而载入文档就避开了这些,可以直接进入内部数据结构了。

Walk vs. selectSingleNode

遍历与selectSingleNode

The fastest way to walk the tree is to avoid the children collection and any kind of array access. Instead, use firstChild and nextSibling:

最快遍历树的方法是避免子集合和任何数组访问。取而代之的,使用firstChild和nextSibling:

function WalkNodes(node)

{

var child = node.firstChild;

while (child != null)

{

WalkNodes(child);

child = child.nextSibling;

}

}

The following table shows the results for the sample test files—walking all elements, attributes, and text nodes:

以下表格显示了样本文件遍历元素、属性和文本节点的测试结果:

Sample

样本

Walk time (milliseconds)

遍历时间(毫秒)

Number of nodes

节点数量

Nodes/second

节点/秒

Ado.xml

243

63,723

262,234

Hamlet.xml

63

12,100

192,063

Ot.xml

660

118,720

179,878

Northwind.xml

33

6,438

195,090

However, if you are looking for something in the tree, a much faster way to find it is to use XPath via the selectSingleNode or selectNodes methods. For example, I entered an XPath expression "//willnotfindanything", which walked the entire tree and got the following results that show the percentage improvement over a brute force tree walk:

但是,如果你要在树中查找某一事物,更快的方法是通过selectSingleNode或者selectNodes方法使用XPath进行查找。例如,输入一个XPath表达式“//willnotfindanything”进行查找,得到结果的显示出同强制的树的遍历相比改进的百分比:

File

文件

selectSingleNode (milliseconds)

selectSingleNode(毫秒)

Improvement (percentage)

改进(百分比)

Nodes/second

节点/秒

Ado.xml

173

29

368,341

Hamlet.xml

11

82

1,100,000

Ot.xml

113

82

1,050,619

Northwind.xml

5

84

1,287,600

selectSingleNode is faster because it avoids the overhead of calling through the COM layer. Instead of thousands of calls to firstChild and nextSibling, it works by one single call.

selectSingleNode更快是因为它避免了通过COM层调用的总开销。它不必上千次调用firstChild和nextSibling,只需要一次调用就可以了。

Now, this comparison is really not equal: On one hand, selectSingleNode was doing less walking, because it didn't need to look at the text nodes—it could just skip right over them. On the other hand, selectSingleNode was doing more work, because it was comparing each nodeName with the name specified in the query. But you get the idea. In general, selectSingleNode reduces the amount of DOM code you need to write and gives you a noticeable performance improvement.

这个比较事实上并不恰当:一方面,selectSingleNode遍历的开销较少,因为它不需要查看文本字节,只需要跳过它们就可以了。而另一方面,selectSingleNode做了更多的工作,因为它要将查询中指定的名字与每一个nodeName进行比较。但是你应该已经得到一些要点了。总的来说,selectSingleNode降低了你要写的DOM代码,可以带来显著的性能提升。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有