RE: XSLT 2.0 function - fastest node comparison
> You could experiment to see if reversing the conditions: > > $ranges/range[@to >=$char][@from <= $char] > > is any faster: this will only do one test on the ranges that > are too low, rather than two (assuming that Saxon searches in > forwards order, which it does). Still linear, but potentially > up to twice the speed. That's a very good tip! It does speed up the execution time by around 100ms just by swapping the predicates around. Out of interest, I've implemented a binary search function which works out slower than the sequential search, but this is probably due to the fact that there are only 94 ranges to compare against... <xsl:function name="f:isInRange" as="xs:boolean"> <xsl:param name="char" as="xs:integer"/> <xsl:param name="low" as="xs:double"/> <xsl:param name="high" as="xs:double"/> <xsl:variable name="middle" select="number(floor(($high - $low) div 2))" as="xs:double"/> <xsl:variable name="currentNode" select="$ranges/range[middle]"/> <xsl:sequence select="if ($currentNode[@from <= $char][@to >= $char]) then true() else if ($high > $low) then if ($char > $currentNode/@from) then f:isInRange($char, $middle, $high) else f:isInRange($char, $low, $middle) else false()"/> </xsl:function> I will use the linear search technique with the @to predicate test first as Mike suggested as that's fastest, but comments welcome on the function... Interestingly, using 'cast as' on $middle to narrow it to an integer causes the execution time to increase by 250ms, so I've left them all as doubles (the positional predicate in $currentNode doesn't appear to mind using a double). cheers andrew
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format