xsl-list
[Top] [All Lists]

Re: [xsl] A super-efficient way to compute the sum of A[i] * B[i] for i=1 to n?

2020-05-09 08:55:32
I doubt you'll find much improvement on this.

You could cut out the call on number() and rely on implicit conversion, but I 
doubt it makes any difference.

You could factor out the expressions ($A/col) and ($B/col) into variables 
declared outside the loop, which might make a difference: finding the Nth child 
of an element might well take time proportional to N, whereas finding the Nth 
item in a sequence held in a variable is likely to be constant time. But it 
depends on the processor, of course. Measgre it and let us know the results.

A significant part of the cost is likely to be string-to-double conversion, and 
there's no way of avoiding that. 

Michael Kay
Saxonica


On 9 May 2020, at 12:59, Costello, Roger L. costello(_at_)mitre(_dot_)org 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi Folks,

I need a super-efficient way to compute the sum of A[i] * B[i] for i=1 to n.

For example, suppose A is this:

<row>
   <col>0.9</col>
   <col>0.3</col>
</row>

and B is this:

<row>
   <col>0.2</col>
   <col>0.8</col>
</row>

I want to compute:

(0.9 * 0.2) + (0.3 * 0.8)

Here's one way to do it:

sum(for $i in 1 to count($A/col) return number($A/col[$i]) * 
number($B/col[$i]))

I suspect that is not the most efficient approach.

What is the most efficient approach? I will be doing hundreds of thousands of 
these computations, so I want to use the most efficient approach.

/Roger

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>