On 2012-05-18, Paulo Moura <
pjlm...@gmail.com> wrote:
> On May 16, 6:06 pm, Jaroslav Dobrek <
jaroslav.dob...@gmail.com> wrote:
>> Hello,
>>
>> I tried to time programs, i.e. to count the number of logical
>> inferences in Prolog scripts. I didn't manage to use a timing
>> predicate in Yap at all (use_module(library(swi)) only produced error
>> messages.).
>
> You don't say which YAP version are you using... recent ones provide a
> time/1 built-in predicate. Same for recent versions of XSB. But
> neither YAP or XSB will give you the number of inferences as with time/
> 1 in SWI-Prolog.
>
>> In Swi, there is the predicate time/1, but it doesn't seem
>> to be usable inside scripts.
>>
>> I would like to do something like:
>>
>> main :-
>> time(my_program, T),
>> write(T),
>> nl.
>>
>> Is this possible with Swi or some other Prolog?
>>
>> I can't do this in the Prolog shell, because I want to test a very
>> large number of (automatically generated) programs.
>
> The time/1 predicate in SWI-Prolog seems to write to user_error. Maybe
> there's a way to redirect it to e.g. a file for collecting the
> results?
It uses print_message/2, which can be hooked using message_hook/3.
Way easier is simply this (assuming G succeeds deterministically).
inferences(G, Count) :-
statistics(inferences, Inferences0),
G,
statistics(inferences, Inferences1),
Count is Inferences1 - Inferences0.
> Counting the number of inferences can also be done using a meta-
> interpreter, assuming you can afford the slowdown.
There is some point in that. The statistics counts passes through the
call and redo ports, but not everything really goes through these ports.
Some predicates are inlined in the VM. Others are mapped to C-functions,
etc.
Cheers --- Jan
P.s. Simply type ?- edit(time/1). to see the implementation. That
easily lets you figure out the above alternative yourself.