Subject: [xsl] How smart are the XSLT processors? Are there any XSLT processors that convert tree-recursive functions into efficient iterative procedures? From: "Costello, Roger L." <costello@xxxxxxxxx> Date: Tue, 27 Apr 2010 19:27:11 -0400 |
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. 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>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] presenting xml elements a, Michael Kay | Thread | Re: [xsl] How smart are the XSLT pr, David Carlisle |
RE: [xsl] presenting xml elements a, Michael Kay | Date | Re: [xsl] How smart are the XSLT pr, David Carlisle |
Month |