Slashdot Mirror


OCaml vs. C++ for Dynamic Programming

jcr13 writes "OCaml is nearly as fast (or sometimes even faster) than C, right? At least according to the Computer Language Shootout [alternate] (OCaml supporters often point to these shootout results). My results on a real-world programming problem (optimizing a garden layout using dynamic programming) disagree. On one particular problem instance (a garden of size 7x3), my C++ implementation finished in 1 second, while the OCaml implementation was still running after 16 minutes. Bear in mind that my OCaml implementation was dramatically faster than my equivalent Haskell code. It seems that if you program using a functional style in OCaml (which I did, using map, filter, and other recursive structures in place of loops), it is quite slow. However, most of the shootout OCaml programs rely heavily on OCaml's imperative features (unlike Haskell, OCaml doesn't force you to be a functional purist). If you write OCaml code that is isomorphic to C code, it will be fast---what about if you use OCaml the way it was meant to be used?"

3 of 161 comments (clear)

  1. Re:Hmmm ... by Dahan · · Score: -1, Flamebait

    I know this is Slashdot, but you really should read the article, even if you haven't grown up yet. Your question, "Any chance your implementation is not memoizing?" will be answered once you read the article.

  2. Speed alone by idiotfromia · · Score: 0, Flamebait

    If you're just looking for speed, try assembly. I hear all the good programmers are using it these days.

  3. Re:Hmmm ... by Dahan · · Score: -1, Flamebait
    Having read the article and looked at your Ocaml code

    Glad to hear it... I guess the next step is to look at my name and/or email address. After that, you should see that it's not my article or my Ocaml code.