I have found a situation where the number of times an expression is evaluated 
is greatly multiplied when the variables are declared in the prolog versus 
using the "let" keyword. There's something going on here that I just can't 
figure out. 

I know the code is a little strange, but it is the simplest example that I 
could come up with that would definitely show the differences:

Using declare:

declare variable $set := (1,2,3,4,5,6,7,8,9,10);
declare variable $set-length := fn:count($set);

let $value := 
        for $s at $i in 1 to 100000
        return $set[$set[$i + $set-length div 2]]

return $value

Total time: 16 sec
Number of times "$set-length div 2" is evaluated: 9999990

----------------------------------------------------------------------------------

Using let:

let $set := (1,2,3,4,5,6,7,8,9,10)
let $set-length := fn:count($set)

let $value := 
        for $s at $i in 1 to 100000
        return $set[$set[$i + $set-length div 2]]

return $value

Total time: 0.4 sec

Number of times "$set-length div 2" is evaluated: 100000

----------------------------------------------------------------------------------

And check this out, using declare but moving the index expression out to a let:

declare variable $set := (1,2,3,4,5,6,7,8,9,10);
declare variable $set-length := fn:count($set);

let $value := 
        for $s at $i in 1 to 100000
        let $index := $i + $set-length div 2
        return $set[$set[$index]]

return $value


Total time: 0.37 sec


Number of times "$set-length div 2" is evaluated: 100000

----------------------------------------------------------------------------------

So what's going on here? I'm using MarkLogic 5

-Ryan
                                          
_______________________________________________
General mailing list
General@developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to