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?"
So the OCaml compiler is supposed to figure out that there's a dynamic programming solution to the problem you have specified using higher order operators? That's a great idea. Let me know when you work that out cause I'd love to be able to just write a specification, press a button and get an optimised program. If you do manage to make this though, be sure not to tell those bastards who pay our salaries, they'll probably think we're not working anymore.
How we know is more important than what we know.
That difference is so dramatic that I wonder if you made a mistake in your functional implementation? Or is there something specific about your dynamic program that makes trouble?
Dynamic programming depends basically on memoization (not "memorization", before someone complains about my typo) which inherently means preserving some state. If you don't preserve state, it becomes a good old, likely exponential time, recursive program. Any chance your implementation is not memoizing?
That doesn't mean that ocaml is slow. It means that dynamic programming in a functional programming style in an eager programming language requires a lot of care, or perhaps should even be avoided. A result that wouldn't surprise me the slightest.
You know I've implemented some real world applications recently for a contract job, and the Ocaml applications are actually faster than the C++ equivalents using the STL. So you mileage may vary based on your problem set (or Ocamlfu as the case may be). As for how Ocaml is supposed to be programmed, there's a reason Ocaml supports imperative programming, because you should use the form that is most efficient for your problem. Some programs benefit from a functional approach (and it helps if you implement properly tailrecursive functions, and make intelligent use of arrays and other block data structures). So one can argue Ocaml is not particularly functional, because it more pragmatically allows for multiple styles of programming. You can do functional programming in C++ actually, but depending on the optimizer you end up with stack issues. My experience with maintaining and extending the Ocaml programs over large C++ code bases is a world of difference. Ocaml wins hands down. Even extending the language to support 3rd party libraries, doesn't place sufficient barriers to maintenance. But ymmv... as with all things.
I'd like to see the Haskell sources for comparison.
http://minorgems.sf.net/Haskell.hs doesn't exist, though the the C++ and OCaml code are there.
Shae Erisson - ScannedInAvian.com
Usings lists in all circumstances because its functional is not appropriate. Show the ocaml implementation using arrays or an c++ implementation using linked lists for a valid comparision.
The strength of Ocaml is the flexibility it provides to a developer. If your solution is more elegantly coded using imperative constructs, then use them!D has no support though. At least when compared to other modern languages (C/C++, C#, Java, Perl, Python, etc.). It really not all that different from C++. It has some better syntax and extra features, but it's simply non-standard and the tiny improvements aren't worth the risk of using something unproven. It is a dead end.
In the same kind of vein, has the code been profiled? I'd quite like to see where the time is going.
On the flip-side, there are plenty of languages which were "official" standards, once, but which are now all but dead. ADA was the "standard" language of the DoD, for example, was (and is) very well supported, but is simply incapable of competing with the other languages out there.
I guess what I'm saying is that there are plenty of languages which were once "unproven", "non-standard" and "unsupported", but which have proven themselves over time. Most languages start that way. There are also plenty of languages which are "proven", "standard", "supported" and total failures in the real world, for whatever reason.
This is not to say D is "good", or should be used. Rather, it is to say that the success and failure of computer languages is unpredictable. The same is true of any computer technology or IT-based market. The only way to survive, in this industry, is to have the skills people want both now and next week. D may be the branch that C follows, the way C is the branch of B that survived, which in turn was the branch of BCPL that survived. I'd rather learn a skill I don't need than lack a skill that's essential. Once you hit a crisis point (dot-com bursts, for example), it is too late to learn something new.
It's a small world and it smells funny; I'd buy another if it wasn't for the money; Take back what I paid (SoM)
I've seen different C compilers with all speed optimizations enabled generate code for the same problem that differed in performance by a factor of 20. This was not consistent with other benchmarks I had seen on the web comparing the same compilers. The benchmark was an html compressor. I'd share more, but unfortunately the code was automatically deleted by XP's chkdsk after a minor crash.
One algorithm is sometimes not enough to accurately judge between compilers. But I guess if it's OCaml, then sure, C++ is faster.
I know you think you're joking, but you're actually right -- that's what the good programmers do. In my application, the performance critical routines (on the order of a dozen or so) are hand coded in vector assembler (Altivec on Mac, and MMX and SSE on Intel), and are about 3 to 4 times faster than the most optimized C or C++ implementation of those algorithms. If you have code that is vectorizable, and especially if it is doing saturated small integer math, (blending, resampling etc), you can do way better than any existing compilers by hand coding in assembler.
Ian Ameline
But now it all seems to be about Arrows. AAARRRGGGHHH!
Too bad. Haskell really is one of the coolest languages around.
There are actually quite a few clean-room Java implementations. The GNU Classpath homepage lists 14 Java VMs that use the Classpath clean-room implementation of the Java class libraries.
For the record, Classpath is 99.79% function complete relative to JDK 1.1 according to the comparison linked from the home page. The numbers drop off with JDK 1.2. Some bits of Swing are missing and most of the org.omg.corba hierarchy is excluded for reasons to do with the OMG's copyright notice.
Ameline:
.NET distribution... they beat me. By a healthy margin. Almost 20% in the inner loop!
Um... not true.
I consider myself an expert assembly level programmer. I recently took on a job optimizing some C code. The code was the back end bi-level compression code in a JBIG library. Bit twiddling, and updates to a compression table.
No big deal, I though. Unrolled the inner loop, and extracted patterns (as in "this case cannot possibly happen on the next n bits being 0, so bulk update and skip forward"). I wrote a program to generate the cases of interest, and tested.
Very good results -- client is happy. I converted to assembler, hand scheduling instructions. On a Pentium II (400Mhz) that I used for my speed test machine, I handily beat GCC (3.something). By a factor of 2, which was expected. Compiling the code using Microsoft C from the
Analyzing the result and checking Intel documentation... It shouldn't have been that way. However, Microsoft may have better connections with Intel, and therefore better documentation on the processor.
On code that is vector code -- yes, I will agree with your point. But I think that advantage will fall by the wayside as compiler vendors implement C99 features, and auto-vectorization.
But don't sell the current crop of C compilers short - in particular, MSVC 7 is VERY VERY good at generating x86 code.
Ratboy (hanging up the assembler badge, sadly).
Just another "Cubible(sic) Joe" 2 17 3061
You apparently don't know what "dynamic programming" means. Dynamic programming is a general technique, usually applied to search, which caches the results of executions on problem subsets and then reuses those results when those subsets are encountered again. It has nothing to do with dynamic typing, or dynamic code, or whatever you're thinking about.
To put it extremely simply, dynamic programming means hash tables, although that's not really a complete definition.
Can we see your Haskell code?
Haskell is not known for raw speed, but dynamic programming is probably the one thing it does well, thanks to lazy evaluation. You fill a CAF with unevaluated function calls, and the language engine does the rest. It won't be as fast as the hand-crafted C++ version, most likely, but if your O'Caml code is anything to go by, it might be able to be improved.
sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});
how about intel's compiler?
One of the joys of programming in ML is that you can write most of your code in a really nice, functional way, and (if necessary) put in the effort to write a tight inner loop, perhaps in an imperative style for speed. I don't see this as a disadvantage, and ML compilers often do a better job of optimizing such loops than C compilers, in part because of more information being available in the type system. (And if they don't, it's trivial to invoke C subroutines.) Also, if performance is really an issue, you might try mlton (which is for SML, very similar to Caml); its whole-program approach often produces significantly better code than O'Caml.
However, as an every-day ML user I find it very unlikely that your program would be a thousand times slower if you're using it "the way it's meant to be used." I am guessing that your implementation is asymptotically worse, since using map and fold correctly should really only be a constant factor slower than C, at worst. (mlton can often inline and optimize these into essentially the same code you'd write in C!) How about posting your code?
Since the job was T&M, I didn't have the freedom to explore Intels compiler. Sorry -- and it would have been very interesting.
Ratboy.
Just another "Cubible(sic) Joe" 2 17 3061
Ironically, the OCaml version posted uses a list rather than a hash table. I'm busy making dinner right now, but I'm certainly interested in seeing if that can be improved upon.
Perhaps this is a case of problem solving by public contradiction?
I Browse at +4 Flamebait
Open Source Sysadmin
You should use the Buffer module, or String.concat:If there is a lot of those mistake, no wonder it is so slow...
Well, I tried the code and on my computer it ran in about 10 seconds (ocamlc/bytecode) and in about 7.6 seconds (ocamlopt/machine code). So, what's the problem this discussion is about?
I see nothing wrong in your C++ version, while your ocaml version clearly sucks: you are memoizing using a complex key, and an association list, meaning that accessing memoized information costs a lot.
e n2.ml
If you are concerned by performance, you should use a complete cache, like in your C version.
FYI, I uploaded an ocaml translation of your C code. It doesn't use mutable state except for memoizing, and uses pattern-matching on lists, and recursion rather than for loops, but otherwise it follows closely your code. Performance should be very similar.
http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/gard
You wrote it in pure C! You are comparing Ocaml with C, not C++! You are just using the C in C++.
Try writing your program fully using OO and templates. I dare you.
Another thing: You OCaml code is shity. Im not asking you to tinker with optimisations, but just to use the correct data structures.
It's not fair compare lazy OCaml code to simpleminded C code.
I intend to live forever. So far, so good.
> Here's a laundry list of why your O'Caml program in inefficient:
>
> 1. You use lists. Lists aren't designed to be fast (computationally)
> to use. They're designed to be fast (programmatically) to use. You'll
> be hard pressed to find a production, speed-sensitive Lisp or O'Caml
> program that uses lists.
Okay... but here's my point: Every single example that shows how elegant Haskell and OCaml are uses lists. The 4-line Quicksort example for Haskell uses lists. All of the code that demonstrates easy reuse of functions and functions taken as arguments uses lists (like how easy it is to implement quite complicated algorithms using only map and filter, for example).
So, proponents say "Everyone should use functional languages because they can express complicated problems in elegant ways and result in cleaner, more reusable code."
But what you're saying in #1 above is that in "production," speed-sensitive code, no one is using lists... this would mean that no one is using map, filter, or any other pieces of reusable primitive code. So, they are instead all using mutable data structures... I.e., they are programming with side-effects and loops (random access instead of recursion, even when ever element of an array/list needs to be accessed/processed).
That was my point exactly. If you write elegant OCaml code using all of the lovely (and I mean lovely, really) tricks that they present when they demonstrate why OCaml is cool, you end up with code that is too slow to use in the real world.
I would say that my C++ (or most would call it C) implementation is elegant enough... easy to understand... no messy optimization tricks. Sure, I'm not using objects and templates everywhere, but these structures are hardly needed to solve this simple problem.
> 2. Practically none of your functions are written tail-recursively.
Good point.
> 2.5. You use a list append (@) inside a loop (generateStates).
> List.append is O(m), where m is the length of its first argument. If
> you write an implementation, you'll see why. It probably doesn't make
> much of a difference here (generateStates is only called once) but it's
> something to watch out for.
Of course, as you point out, generateStates has almost no effect on the running time. However, I wonder how you might implement that in an elegant way in OCaml without @. In C, I just looped over all numbers between 0 and 2^stateLength and converted the bit representations for the numbers to cell on/off states.
> 3. For Pete's sake, man, you're using an association list for your
> memos! Surely you know that lookup in an association list is O(n) in
> the size of the list.
I simply Googled for "memoization Ocaml" and found that code:
http://www.emeraldtiger.net/modules.php?op= modload &name=News&file=article&sid=9
The author pointed out how "sweet" polymorphism is... one block of code that can be used to memoize any function. Sweet indeed, and it certainly sped up my OCaml code a lot (without memoization, it was so slow as to be intractable for anything larger than about 4x4).
So... maybe you can re-write higher-order memoization code using more efficient data structures? I would love to see that code, and I'm sure the OCaml community would benefit from having that in their toolbox.
I agree that the memoization code is probably the problem in the OCaml version. However, this code came directly from the OCaml community and was the *only* example of memoization in OCaml that I could find.
For Haskell, I used an infinite list of results that was filled in lazily as the results were needed. This also sped up the algorithm dramatically. However, I cannot get a Haskell compiler to compile itself on my platform, so I was testing all code in the Hugs interpreter, which made it too slow to be practical. Isomorphic compiled OCaml code was hundreds of times fast
Look at the shoot-out with only tests that are all available for C, C++ and O'Caml are included and weighted for both CPU and Memory. It paints quite another picture.
moot actually means debatable. However, in the expression "moot point" it seems to be used in the sense "tangential/unnecessary point"
Have you considered updating that web page - keep the content you have there now, but possibly tack on some of the feedback from here that shows that yes, a very, very bad implementation of an algorithm will run slowly, regardless of the language. It would be a shame of someone ran across the page seeking a realistic comparison, and didn't look deep enough to realize that this "study" is basically the equivalent of comparing the gas mileage of two makes of automobile without informing the readers that one car had a semi-truck chained to the back of it during the trials.
"Bellman". Google "Bellman" "dynamic programming".
Just to get some historical context.
Interestingly, "dynamic programming" wasn't really even meant as a computer-programming technique.
The insert example isn't great. Despite using the same name and similar end results, these are unlikely to have the same performance characteristics, which is likely to be a significant difference in practice.
However, consider a simple function such as map, which fundamentally takes some structured data, and produces a new set of data with the same structure, built from the original elements acted on by some function. This concept is applicable to a wide range of data structures, and writing it polymorphically (not generically) just makes for code that's easier to read and less memory work for the developer.
A more advanced example might be the use of a fold style function directly on lists. A similar concept could be applied to a tree, but you'd have to have some intermediate functionality that defined the ordering you were using to traverse your tree. Still, it can be useful to separate the concepts of the ordered algorithm (fold, or whatever more complex function is using it) from the ordering algorithm (traversing breadth-first, depth-first, etc.), from the connecting and terminal functions used by the fold algorithm specifically but potentially useful elsewhere as well.
If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
Yeah... I got dynamic programming and memoization confused.
If you want to program in a functional style, and you need lazy evaluation, you're going to find the standard library that comes with the compiler somewhat limited.
I wrote some extensions for programming in OCaml in the functional style. Check out the OCaml NAE project, and look for the Core Foundation (Cf) package.
jhw
That's a very important point, and I think the effect of the community around a programming language (or indeed a whole paradigm -- functional, OOP, etc.) is often underestimated.
Just look at PERL: it has its merits, but in fairness clarity to newcomers is rarely listed as one of them. And yet, because asking a question on a PERL group tends to result in joyous cries of "TMTOWTDI" followed by an enthusiastic discussion of the relative merits of 17 different ways to solve your problem, even the most unaware newbie is likely to find help and education simply by asking politely.
In contrast, some languages (C and C++, for example) tend to invite technical perfectionists, who are happy to help but only if you're Doing Things Properly(TM). Others (functional languages, LISP dialects, etc.) tend to have quite academic communities, whose response to newbies, sadly, can be less than welcoming.
Now, take a look at those languages I listed, and ask yourself which ones have been the bigger success stories...
If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
I did explicitly say "if it's vectorizable", and "particularly if it uses small integer saturated math". I doubt any autovectorization will recognize the conditionals necessary for saturated math and make efficient use of MMX instead. (I worked on the back end of an optimizing compiler at IBM for 6 years, so I know a bit about how compilers work :-)
Ian Ameline
I've tried 'em all. Intels is good, but when it comes to toe sort of code I was talking about in my original post, you can still get a factor of 2 or 3 over whan it can do with scalar code, if you havd code a vectorized MMX or SSE version.
Ian Ameline
Check out comp.lang.lisp to see for yourself. No one appreciates people asking them to do their work for them but, they're helpful to people earnestly trying to learn.
Someday we'll all be negroes
Here's my first attempt at a version which performs decent in Haskell. It doesn't do any memoization, and isn't particularily dynamic. It ended up being only 35 non-blank lines of code. The 9x3 case takes 26 seconds to complete on my Athlon 1600 compiled with GHC 6.4. Erg. You'll have to grab a copy here as /. thinks my Haskell code has too many 'junk' characters.