BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
Performance Tuning XSLT - Part 2
Performance Tuning XSLT - Part 3
Performance Tuning XSLT - Part 4
Using XSLT to produce SVG (3)
XPath as parameters
Table with more than one column
Create an N-Column Table from XML node-set
Finding ""the corresponding node"" in a parallel subtree
Get the last item from a delimited list
Limitation to Muenchian method.
Find the number of tokens or words in a string
Randomization of node-sets or node-lists
XPath Analyzer
Finding the Maximum Date
World Cup 2002 fever hits XSLT!!
Using XSLT for DHTML Menus
RSS / RDF Newsfeed Reader
Hierarchical menu from Database + XSL to Html
XSLT syntax hightlighter
XSLT Stylesheet for listing namespaces used in a document


 
 

<< XQuery.NET and XML >>


By Martin Rowlinson (aka Marrow)
First Posted 03/07/2002
Times viewed 173

Performance Tuning XSLT - Part 1


Summary Using variables to tweak the performance of your XSLT

This article briefly discusses the ways in which XSLT variables can be used to enhance the overall performance of XSLT stylesheets.  Obviously, these performance enhancements only become relevant when handling large amounts of data - although the cumulative effect may even be sufficiently beneficial to use on moderately sized input XML data.

Using the XML listed below - say you wanted to output all of the data for each <data> element plus one peice of static data from another part of the input XML (in this example, the <static_data> node).  The XSLT for this would be fairly straight forward, e.g.

Example A:-
<?xml version=1.0?>
<xsl:stylesheet version
=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<
xsl:template match
=/>
  <
xsl:for-each select
=root/data>
    <
xsl:value-of select
=/root/static_data[1]/>
    <
xsl:value-of select
=./>
  </
xsl:for-each
>
</
xsl:template
>
</
xsl:stylesheet
>

Although more careful inspection might suggest that placing the static data from the /root/static_data[1] element into a variable might enhance the performance, e.g.

Example B:-
<?xml version=1.0?>
<xsl:stylesheet version
=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<
xsl:template match
=/>
  <
xsl:variable name
=something_static select=root/static_data[1]/>
  <
xsl:for-each select
=root/data>
    <
xsl:value-of select
=$something_static/>
    <
xsl:value-of select
=./>
  </
xsl:for-each
>
</
xsl:template
>
</
xsl:stylesheet
>

The performance results of these two stylesheets is quite different (comparisons made using MSXML3 with MSXML4 figures in square brackets):-
  Example B has a 48% [55%] reduction in time taken over Example A.
The logic of this performance increase is that the XSLT has been saved from continually having to re-lookup the value of the <static_data> node.

In Example B the data from the <static_data> node is stored as a node-set in the variable - but we are repeatedly outputing this value as a string.  So what about if we 'cast' the value to a string in the first place (when the value is placed into the variable) and see if this gives a better result, e.g.

Example C:-
<?xml version=1.0?>
<xsl:stylesheet version
=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
<
xsl:template match
=/>
  <
xsl:variable name
=something_static select=string(root/static_data[1])/>
  <
xsl:for-each select
=root/data>
    <
xsl:value-of select
=$something_static/>
    <
xsl:value-of select
=./>
  </
xsl:for-each
>
</
xsl:template
>
</
xsl:stylesheet
>

The performance results of these two stylesheets is:-
  Example C has a 39% [22%] reduction in time taken over Example B.
And the cumulative results of the final modification over the first incarnation is:-
  Example C has a 69% [65%] reduction in time taken over Example A.

So the principles of this performance enhancement are:-
* if something static is used multiple times - retrieve it only once;
* cast values placed in variables to a string as early as possible.

Cheers - Marrow
http://www.MarrowSoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.TopXML.com/Xselerator

 

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (1 votes, average 7)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML