Inside MSXML Performance(MSXML效能分析) (4) (轉)

worldblog發表於2007-12-12
Inside MSXML Performance(MSXML效能分析) (4) (轉)[@more@]

First Walk Working Set Delta:namespace prefix = o ns = "urn:schemas--com::office" />

第一次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個相同的如下元素:

  this is a test

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. SingleNode

遍歷與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 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 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 skright 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 a. 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程式碼,可以帶來顯著的提升。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991689/,如需轉載,請註明出處,否則將追究法律責任。

相關文章