[mercury-users] First solution in csharp without try/catch

Mark Brown mark at csse.unimelb.edu.au
Sun Jan 15 23:59:50 AEDT 2012

On 14-Jan-2012, Jeff Thompson <jeff at thefirst.org> wrote:
> On 1/13/2012 8:46 PM, Mark Brown wrote:
>> How does the version
>> using exceptions compare to the handwritten version using yield?
> As above, the times for Mercury code are 0.45 seconds for the baseline and 
> 171 seconds if it commits.  For the "yield" code it is 2.33 seconds for the 
> baseline and 2.03 if it commits after one solution.  As expected, since the 
> "yield" code uses the same infrastructure in both cases, it takes less time 
> to search less solutions.  But the baseline Mercury code is faster than the 
> "yield" code which allocates an iterator object on the heap for every 
> "foreach" loop (as opposed to Mercury's more efficient approach of using 
> continuations).
>                   X > 2    X > 0
> Mercury 11.07     0.45     171
> "yield"           2.33     2.03

Thanks for that - it's useful to see how much yield costs.

Given these results, I'd back the continuations+exception approach rather
than yield, since the exception is thrown at most once whereas the number
of yield iterators can be arbitrarily large.  I.e., the slowdown in the
first column will eventually outweigh the speedup in the second.

Unless of course the search is very simple, in which case it may be
better to write it yourself (see below).

>> Also, how do the savings, if any, compare to the amount of time required 
>> to
>> perform a typical search?  The reason I ask is that the project I work on
>> uses nondet search extensively, but the searches are millisecond-scale at
>> the very least.  Hence microsecond-scale costs of exceptions are not as
>> painful for us as the rule-of-thumb would suggest.
> Can you give a concrete example or a reference for a nondet search so I 
> know we are talking about the same thing?  (All I can think is that you are 
> talking about converting all my code to run in a meta interpreter.)

I mean a much more problem-specific solution than a meta interpreter.
E.g., for the second version of multiPred:

> :- pred test is semidet.
> test :- if multiPred(10, X), X > 15 then true else fail.
> :- pred multiPred(int::in, int::out) is multi.
> multiPred(M, X) :- X = 1 * M.
> multiPred(M, X) :- X = 2 * M.

you could instead write something like:

:- pred search(int::in, int::in, int::out) is semidet.

search(Cutoff, M, X) :-
    ( if M > Cutoff then
        X = M
        X = 2 * M,
        X > Cutoff


mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list