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



On 2008-05-20, Bart Demoen <bmd@xxxxxxxxxxxxxxxxx> wrote:
Christina wrote:

Does minimax algorithm work faster in Prolog than in Java?

Some 10 years ago, when Java was only byte-code emulated, there was a
paper (or tech report) by (I think) Spanish people (UPM probably) who
showed with some benchmarks that Java and Prolog had roughly the same
performance (on some benchmarks, including qsort if I remember
right). I repeated some of those benchmarks myself at that time and it
depended a bit on which Prolog system one uses, and how one writes
particular constructs (if-then-else or multiple clauses with cut for
instance), but Java and Prolog were indeed close in speed.

In the mean time, Java execution technology has moved to JIT with
accordingly higher performance, while Prolog has mainly stayed
emulated. So you should expect that Java implementations are faster
(by an order of magnitude) than Prolog implementations. And that's all
you will be able to compare anyway: one implementation versus another
implementation, not one language versus another language. It's all in
the compiler technology that is used, nothing intrinsic in the
language, at least not for toy benchmarks where "everything is known".

To match the speed of Java (or other procuedural languages with unsafe
arithmetic) you have to do a lot of work. You need some form of JIT
and global analysis. I once benchmarked a little program doing a lot
of integer arithmetic and it turned out it was spending most of its
time in the (C) low level addition and multiplication functions doing
the actual work, just because it needs to do all the overflow checking
switching between C bounded arithmetic and unbounded arithmetic. Only
a global analysis that can do a check at entry of the loop and fall
back to a fast algorithm without these checks if conditions are met.
This seems language dependent. If the language doesn't guarantee safe
arithmetic the programmer is responsible (and cleverly moves the check
outside the loop or `forgets' about it all along), otherwise the
implementation. Ok, the Prolog standard allows for both. SWI-Prolog
implements safe arithmetic.

Another example is merge-sort. Implemented straightforwardly in
Prolog, it will produce a lot of garbage lists in the process. That
way you'll never win using Prolog. Not allowing destructive
assignment makes the language less vulnerable for programming
mistakes, but it also makes (some) algorithms slower. Only analysis
may be able to translate the one into the other, providing both good
performance and safety.

All in all though, performance generally isn't that important. The
simple fact that the slowest implementation of Prolog is the most
popular indicates there are other implementation/community aspects
that matter more :-)

Cheers --- Jan
.



Relevant Pages

  • Re: Managing relations between objects
    ... I agree that an inference engine would be useful in language like Java and ... However, a programming environnement like Prolog, Python and Ruby don't ... automation, procedures and state machine cover most of the needs (I know, ... there is no native state machine in Java nor C#). ...
    (comp.object)
  • Re: SBCL is now faster than Java, as fast as Ocaml, and getting better
    ... times faster than Java, and is finally as fast as Ocaml. ... For purposes of propaganda, i.e. advocating Common Lisp, this is ... exactly the same ratio for each language. ... programming language in the Benchmarks Game?" ...
    (comp.lang.lisp)
  • Re: In which cases/problems is Prolog faster than Java?
    ... showed with some benchmarks that Java and Prolog had roughly the same ... but Java and Prolog were indeed close in speed. ... not one language versus another language. ...
    (comp.lang.prolog)
  • Re: How long it takes to educate Prolog/CLP programmer?
    ... other language" is pretty shallow. ... Now some guys want to apply the same principle to Prolog. ... 20 K lines of Prolog (with CLP) and they want someone to "learn ... years of C++ or Java. ...
    (comp.lang.prolog)
  • Re: Managing relations between objects
    ... However, a programming environnement like Prolog, Python and Ruby don't ... > there is no native state machine in Java nor C#). ... programming language, but it does have a C interface for embedding into ...
    (comp.object)