Re: In which cases/problems is Prolog faster than Java?



On 19 Mai, 23:05, bart demoen <b...@xxxxxxxxxxxxxx> wrote:
Show us your mergesort please. A reasonable mergesort in SWI-Prolog
(not the fastest around) sorts 30000 elements easily.

% MERGE SORT
/* merge_sort(Xs, Ys) is true if Ys is a sorted permutation of the
list Xs.*/
group::merge_sort(List, SortedList):-
length(List, Length),
merge_sort_1(Length, List, SortedList, []).

/* merge_sort_1(N, Xs, Ys, Zs) is true if Ys is a sorted permutation
of */
/* the first N elements of the list Xs, and Zs are the remaining
elements */
/* of
Xs. */
merge_sort_1(0, Rest, [], Rest):-!.
merge_sort_1(1, [A|Rest], [A], Rest):-!.
merge_sort_1(2, [A,B|Rest], C, Rest):- A =< B, !, C = [A,B].
merge_sort_1(2, [A,B|Rest], C, Rest):- !, C = [B,A].
merge_sort_1(N, List, Sorted, Rest):-
N1 is N // 2, N2 is N - N1,
merge_sort_1(N1, List, SortedLeft, TempList),
merge_sort_1(N2, TempList, SortedRight, Rest),
ordered_merge(SortedLeft, SortedRight, Sorted).

/* ordered_merge(Xs, Ys, Zs) is true if Zs is an ordered list
obtained */
/* from merging the ordered lists Xs and
Ys. */
ordered_merge([X|Xs], [Y|Ys], [Y|Zs]):-
Y =< X, !,
ordered_merge([X|Xs], Ys, Zs).
ordered_merge([X|Xs], Ys, [X|Zs]):-
ordered_merge(Xs, Ys, Zs).
ordered_merge([], Ys, Ys).

/* length(Xs, L) is true if L is the number of elements in the list
Xs. */
length(Xs, L):-length_1(Xs, 0, L).

/* length_1(Xs, L0, L) is true if L is equal to L0 plus the number
of */
/* elements in the list
Xs. */
length_1([], L, L).
length_1([_|Xs], L0, L):- L1 is L0 + 1, length_1(Xs, L1, L).

I am running Trinc-Prolog R3b RC1 Trial version.
I have also installed SWI-Prolog and Logtalk, now I'm working on
converting the Trinc-Prolog
version of merge sort into the corresponding SWI-Prolog.
Does minimax algorithm work faster in Prolog than in Java?
.



Relevant Pages

  • Panic, a strange behavior of lisp program
    ... when SORT is called with a parameter that includes some that ... No actually although CLtL ... this apparent mistake in conversion from CLtL to ANSI-CL. ... Web page that lists all such "obvious" mistakes. ...
    (comp.lang.lisp)
  • RE: Incident investigation methodologies
    ... However, what sort of reaction ... Speculation gets you nowhere. ... > malware we encounter. ... > of what makes public lists useful - you can get some ...
    (Incidents)
  • Re: Detailed explanation of how a QuickSort Works
    ... Firstly, if you consider the simple "Bubble Sort" algorithm, it works by running through the entire data set, one item at a time, comparing each item to the previous item and swapping them if they are not already in the correct order. ... by simply running through the entire list just once (and splitting it into two smaller lists) you have cut the sorting time in half. ...
    (microsoft.public.vb.general.discussion)
  • Re: Ordering Products
    ... algorithms. ... lists with constrained item transpositions. ... I think while the built in sort works as a convenience, ... Overall it's about 10 times slower than pythons built in sort for large ...
    (comp.lang.python)
  • Re: Ordering Products
    ... > Kay Schluehr wrote: ... >> It would be interesting to examine some sorting algorithms on factor ... >> lists with constrained item transpositions. ... > I think while the built in sort works as a convenience, ...
    (comp.lang.python)