Lush, OCaml and more, part deux

In a previous blog entry, I did some extremely informal benchmarking with Lush, OCaml, Python and C. I’ve now added two new Python tests: one with Psyco, a JIT-like solution that takes almost no effort to add to existing code, and one with Pyrex, where one can code extension modules in a language that looks just like Python but has types. These modules are then translated to C and compiled into Python usable extension libaries.

With Psyco, one only has to add the lines “import psyco; psyco.full()” to one’s Python code. The performance gains can be huge, but in general are quite modest. It takes far less effort than Pyrex though.

The updated benchmarks table is shown below:

Language Time (seconds)
Octave 2.1.57 7.894
Python 2.3.4 0.459
OCaml 3.08 interpreted 0.229
Python 2.3.5 with Psyco 0.148
Python 2.3.5 with Pyrex 0.064
OCaml 3.08 compiled 0.021
gcc 3.3.4 0.017

The new implementations are available at the usual place.

Lush, OCaml and more

I took a long hard look at the OCaml functional (well, mostly) language this weekend. One of the many interesting aspects of OCaml, is that, in addition to offering an interpreted environment, it comes with a REALLY good compiler. So, you can sit there prototyping your latest numerical trick and when you’re happy, you can compile the code to a blazingly fast native binary.

So, whilst reading up on all this, I remembered a question from the Lush (a lisp-like scientific languages that can also be compiled) FAQ: “How does Lush compare to Matlab/Octave for speed?”. So the author then shows that Lush is like 50 billion times faster than Octave. (Go look.)

I implemented his little benchmark in Python, C, Octave and OCaml and tested the codes on a P4 2.4GHz running Ubuntu Linux. These were the performance figures for harmonic(1000000):

Language Time (seconds)
Octave 2.1.57 7.894
Python 2.3.4 0.459
OCaml 3.08 interpreted 0.229
OCaml 3.08 compiled 0.021
gcc 3.3.4 0.017

For each benchmark, I obviously called the harmonic() in a loop with sufficient iterations to yield accurate timings. For the compiled tests, 300 iterations did the trick. For Octave, 3 was more than enough. You can find my implementations by clicking here.

This was really just for fun, in other words: DON’T TAKE IT TOO SERIOUSLY. Also, the OCaml snippet I wrote is mostly imperative. However, the results reflect what is also shown by The Great Computer Language Shootout: OCaml is a good contender when one needs to write very fast code in a more advanced language than C.