RSS
热门关键字:  数据挖掘  人工智能  数据仓库  搜索引擎  数据挖掘导论
当前位置 :| 首页>编程技术>xml技术>

InsideMSXMLPerformance(MSXML性能分析)(4)

来源: 作者:unkonwn 时间:2005-09-06 点击:

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:

数据挖掘研究院

最快遍历树的方法是避免子集合和任何数组访问。取而代之的,使用firstChildnextSibling 数据挖掘研究院

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层调用的总开销。它不必上千次调用firstChildnextSibling,只需要一次调用就可以了。 数据挖掘研究院

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

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?