Subject: Re: [xsl] How smart are the XSLT processors? Are there any XSLT processors that convert tree-recursive functions into efficient iterative procedures? From: David Carlisle <davidc@xxxxxxxxx> Date: Wed, 28 Apr 2010 00:51:27 +0100 |
Hi Folks,
Below are two implementations of the Fibonacci sequence.
The first version is a direct translation of the definition of the Fibonacci sequence into XSLT -- it is declarative code.
The second version is an iterative approach -- it is imperative code.
The advantage of the first version is that it is easy to specify and understand. It's disadvantage is that, if the XSLT processor is not smart, it is highly inefficient -- Fib(n) grows exponentially with n.
However, a "smart compiler" could transform tree-recursive functions into more efficient iterative procedures that compute the same result -- a smart XSLT processor could convert the first version into the second version.
I ran both versions on Fib(100). The second version finished in less than 1 second. As for the first (declarative) version, after 10 minutes it was still executing so I killed the process.rewriting an arbitrary expression so it's tail recursive is pretty hard (impossible in general and hard to spot the cases where it's possible)
I conclude that, at least for the XSLT processor I used, it does not convert the first version into more efficient iterative procedures. This is sad. I would prefer writing declarative code, but if XSLT processors do not optimize the execution of declarative code then how realistic is declarative programming?
Are there any XSLT processors that convert tree recursive functions into more efficient iterative procedures?
/Roger
------------------------------------------------- Version #1: Tree Recursion ------------------------------------------------- <xsl:function name="ex:fibonacci"> <xsl:param name="n" />
<xsl:choose> <xsl:when test="$n eq 0"> <xsl:value-of select="0" /> </xsl:when> <xsl:when test="$n eq 1"> <xsl:value-of select="1" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="ex:fibonacci($n - 1) + ex:fibonacci($n - 2)" /> </xsl:otherwise> </xsl:choose>
</xsl:function>
------------------------------------------------- Version #2: Tree Iterator ------------------------------------------------- <xsl:function name="ex:fibonacci"> <xsl:param name="n" />
<xsl:value-of select="ex:fibonacci-iterator(1, 0, $n)" />
</xsl:function>
<xsl:function name="ex:fibonacci-iterator"> <xsl:param name="a" /> <xsl:param name="b" /> <xsl:param name="count" />
<xsl:choose> <xsl:when test="$count eq 0"> <xsl:value-of select="$b" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="ex:fibonacci-iterator($a + $b, $a, $count - 1)" /> </xsl:otherwise> </xsl:choose>
</xsl:function>
________________________________________________________________________ This e-mail has been scanned for all viruses by Star. ________________________________________________________________________
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] How smart are the XSLT proces, Costello, Roger L. | Thread | Re: [xsl] How smart are the XSLT pr, Florent Georges |
[xsl] How smart are the XSLT proces, Costello, Roger L. | Date | Re: [xsl] How smart are the XSLT pr, Dimitre Novatchev |
Month |