xsl-list
[Top] [All Lists]

Re: [xsl] Is xsl:for-each "syntactic sugar"?

2010-05-07 12:37:05
It may be that (as Dimitre Novatchev suggests)
for-each may run faster.  And so if you run into performance problems,
consider trying it.  (But remember Jon Bentley's first rule of program
optimization:  don't do it until you know you need to do it.)


I didn't suggest that "for-each" may run faster.

I said that iterative constucts may be implemented more efficiently.
(This is a general statement and we all know that efficiency varies
accross implementations.)

This by no means should be interpreted as a recommendation to choose
<xsl:for-each> over <xsl:apply-templates>.

Thi first rule of optimization is to know what you're optimizing. As
Knuth once put it "Premature optimization is the root of all evil". It
is immaterial what type of construct, iterative or recursive, is used
if an O(2^N) algorithm is selected in one case and an O(Log(N))
algorithm is selected in a second case.

To repeat once again my statement: Choose an iterative construct over
*explicit* recursion, if this is possible.

The emphasis in this statement is on the word *explicit*. I don't
regard <xsl:apply-templates> as explicitly recursive. This means that
in no way the above statement encourages to choose <xsl:for-each> over
<xsl:apply-templates>. On the contrary, I have always recommended
choosing <xsl:apply-templates> except for the only case I know (in
XSLT 1.0), where <xsl:for-each> is really necessary -- when we need to
change the current document so that the key() function is applied in
its context. In XSLT 2.0 <xsl:for-each> could be applied on any
sequence (not only on a sequence of nodes as required by
<xsl:apply-templates), but this will be history with XSLT 2.1  -- a
very welcome news announced today by Dr. Michael Kay.



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play



On Fri, May 7, 2010 at 9:43 AM, C. M. Sperberg-McQueen
<cmsmcq(_at_)blackmesatech(_dot_)com> wrote:
On 7 May 2010, at 00:27 , Markus Karg wrote:


4. Favor recursive functions over xsl:for-each. (True or False)

Totally wrong, as it is much more complex to read and understand as a
simple loop.

Very interesting -- my experience with XSLT is completely different.
I think the use of apply-templates with a mode is much easier to
read and understand than for-each.

For several years, in fact, I consistently found that if I had a bug
in a stylesheet that used for-each, recasting the for-each in terms
of apply-templates with a special mode invariably removed the bug.
In some cases, of course, I knew the bug was in the for-each, but
in a few cases I had no idea where the bug was; since the bug went
away when I eliminated the for-each, I inferred that it had been
there.  Perhaps this is just because I can never remember the
difference between context node and current node, and so make
mistakes.

So I think my advice to XSLT authors would be:  write the form which
seems clearer to you.  If unlike me you find for-each easy to
understand and to write without bugs, then by all means use it!
But if like me you find apply-templates natural and easy, then do not
be frightened away from it by fears that XSLT processors may optimize
it less well.  It may be that (as Dimitre Novatchev suggests)
for-each may run faster.  And so if you run into performance problems,
consider trying it.  (But remember Jon Bentley's first rule of program
optimization:  don't do it until you know you need to do it.)

YMMV, of course.

--
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com
* http://cmsmcq.com/mib
* http://balisage.net
****************************************************************





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>