Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is there a C code that can be written that compiles to assembly which is as efficient (or more) than OCaml? Likely so, and if the author doesn't even attempt to do that, I don't find their argument convincing.

That is the point of the author, you certainly can achieve the same speed in C but you have to do extra work because the compiler is unable to do things like discovering the absence of aliasing. So you have to manually build this into the code which in turn makes it more complex.



Yeah, the thing about low-level languages like C is that they are so tied to the machine -- and CPUs are now immensely complex beasts --- that your program, to be fast, has to be written to take advantage of that machine's characteristics: using target-specific intrinsics (e.g. SSE, AVX) or designing data structures around the size of the cache line.

Whereas a higher-level language can truly abstract over of all these things, and perform target-specific optimizations where it sees them.


OK, so let's turn it around: are there cases where an expert user who wants to get the most out of the machine can generate faster code in OCaml than the fastest implementation an experienced C author can write?

I don't care about the average case- I care about the maximum case (because I work on performance sensitive code).


The fastest code possible is determined by the underlying hardware and is ultimately independent of the language you use. The only reasons that you would not attain this speed limit are that you are either not aware of the best machine specific implementation, that you don't know how to express it in a given language or that a given language does not allow expressing it.

Chances are good that even really good developers are not aware of the best implementation because it might depend on pipeline latencies, constraints on which instructions can execute in parallel and so forth. That is what compilers are good at and I don't think there is an in principle difference here between C and OCaml.

The other two points are more involved. On the one hand C offers, for example, things like intrinsics and seems generally more apt to control what machine code will get generated as compared to OCaml. On the other hand C is less expressive when it comes to conveying higher level semantics to the compiler to inform its optimization decisions.

So if you are aware of the optimal implementation - which, as mentioned above, you may not be - you are in a good position with C to get (close to) the desired result if you put in the extra effort to express the semantic details. On the other hand OCaml will certainly make it harder or even impossible to express your desired machine specific implementation. But then again the OCaml compiler can use information not (easily) available from C programs to guide its optimization and could come up with a sequence of instructions better than what the developer could think of.

In principle OCaml seems to have an edge over C because of the stronger semantics making it easier for the compiler to find a good implementation possibly beating the human. On the other hand, if the compiler does a bad job and you realize it, then C probably offers you the better tools to force the compiler to honor your will.

To finally answer your question, it seems highly unlikely that there are cases where one could in principle not write a C program as fast or faster than the equivalent OCaml program. On the other hand it seems not so unlikely that a developer might not be aware of what that C code would have to look like.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: