Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!satisfied.apocalypse.org!news.mathworks.com!uunet!newsflash.concordia.ca!CC.UMontreal.CA!IRO.UMontreal.CA!tarau
From: tarau@IRO.UMontreal.CA (Paul Tarau)
Subject: Re: If-Then-Else (a Performance Model example)
Message-ID: <D6A7D8.4qC@IRO.UMontreal.CA>
Sender: news@IRO.UMontreal.CA
Organization: Universite de Moncton, Canada
References: <SVERKER.95Mar27192329@goodyear.sics.se> <3l8bg9$s6g@goanna.cs.rmit.edu.au> <3lc3q4$1rf@hitchcock.dfki.uni-sb.de>
Date: Fri, 31 Mar 1995 01:41:31 GMT
Lines: 75

In article <3lc3q4$1rf@hitchcock.dfki.uni-sb.de> vanroy@dfki.uni-sb.de (Peter Van Roy) writes:
>Here's an example to show that performance modeling of Prolog
>is not as easy as one might think.
>
>The query square(N) creates a list of length N containing 1's,
>and then sums the list.  The surprising fact is that it has 
>runtime O(N^2)!  
>
>   makelist(0, _, L) :- !, L=[].
>   makelist(N, X, [Y|L]) :- N>0, N1 is N-1, makelist(N1, X, L), X=Y.
>
>   sumlist([],    In, Out) :- In=Out.
>   sumlist([X|L], In, Out) :- Mid is X+In, sumlist(L, Mid, Out).
>
>   square(N) :-
>        makelist(N, X, L),
>        X=1,
>        statistics(runtime,_),
>        sumlist(L, 0, S),
>        statistics(runtime,[_,Time]),
>        write(Time), nl.
>
>I leave it up to the readers to figure out why this program
>is inefficient :-).  Hint: the run-time stack size has nothing 
>to do with it.

Well, it seems that this is a very WAM-centric world... :-)

That's linear in BinProlog 3.30! Try:

range(Min,Min,Max):-Min=<Max.
range(I,Min,Max):-
        Min<Max,
        Min1 is Min+1,
        range(I,Min1,Max).


go:-range(I,1,20),N is 5000*I,square(N),fail.

% bp.sparc.sunos -h10000 on a Sparc ELC with SunOS 4.1.1

?-go.

17
50
100
117
150
167
200
250
266
300
317
367
383
417
433
466
500
533
567
600

With a classical WAM like SICStus:

3100
13180
36710
74650
.... 

Paul Tarau

P.S. Hint: See "The Wonder Years...", section on BinProlog :-)
