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.

5 Comments to “Lush, OCaml and more”
November 16, 2004
So how did Lush do on the above mentioned test ?
November 16, 2004
I haven’t taken the time to figure out how to adapt the Lush code in the FAQ to do multiple loops and do timing over these loops, so I have no figures for it on my machine. If you have time to change the Lush code, I’ll be glad to try the code on the same test machine.
FWIW, the Lush author’s benchmarks for Octave are roughly twice as slow as what I show, which means that my machine should do the Lush bench in about 0.025 seconds.
November 16, 2004
I suspect that this is a biased test and that the research firm involved was paid a large amount of money to misrepresent statistics in a misguided effort to discredit Octave for personal financial gain.
August 21, 2005
I get the following on a P4 2.6GHz, Mandrake 10.1 (gcc 3.4.1):
Octave: 6.44s
Python 2.3.4: 876-911ms
Python-pyrex: 65ms
Ocaml: 213ms
Ocaml-opt: 17.7ms
Lush: 816ms
Lush(inlined): 16.6ms
Lush(compiled): 16.6ms
C: 16.7ms
(see Lush’s documentation on the harmonic-function as it is used as an example in the introductory part)
August 21, 2005
Wow, cool! Also see a later post that includes Psyco and Pyrex benchmarks: http://cpbotha.net/weblogs/cpbotha/archives/001744.html