[comp.lang.prolog] Collecting timing statistics

plummer@juliet.cs.duke.edu (David J. Barker-Plummer) (06/23/89)

Can anyone explain the following phenomenon which occurs when I try to
collect runtime statistics for a program.  I am running Quintus 2.3 on
a SUN Workstation running SunOS 4.0.

I have written the following code for timing a goal:

	runtime(Goal, Time) :-
		statistics(runtime, _),
		call(Goal),
		statistics(runtime, [_,Time]).

Wishing to take the average time over a number of runs I implemented
the obvious (to me) recursive procedure which calls the goal some
specified number of times, collecting the returned runtimes into a
list (ensuring that a new copy of the goal is used each time).  To my
surprise, the list of runtimes returned were in increasing order, with
frequently a factor of two increase over the list.

Thinking that the timing procedure was somehow adding to the runtime,
I reimplemented the collection procedure to be a failure-driven bounce
which runs the Goal, asserts the runtime and then fails back to before
the call to the goal to repeat the specified number of times.  Still
the reported runtimes increase for later runs, and still major changes
in the reported results.

My questions.  Can anyone explain this phenomenon? and Can anyone
supply an approved method of collecting runtimes to be averaged.  In
case the description of the above two procedures are not clear, they
are appended.

Thanks for any help -- Dave

---- Version 1 ----

rec_collect_times(_, 0, []).
rec_collect_times(Goal, N, [Time|Times]) :-
	N > 0,
	copy_term(Goal, GoalCopy),
	runtime(GoalCopy, Time),
	M is N-1,
	rec_collect_times(Goal, M, Times).

---- Version 2 ----

%%%%    Exceeding Ugly Code Alert    %%%%

fdb_collect_times(Goal, Count, Times) :-
	nat(N),
	((N =< Count -> 
	     ( runtime(Goal, Time),
	       assert(time(Time)),		
	       fail ) )
	;
	bagof(Time, time(Time), Times)).

nat(1).
nat(N) :- nat(M), N is M+1.

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

Dave Barker-Plummer			plummer@cs.duke.edu