0

So I have some cool Image Processing algorithm. I have written it in OCaml. It performs well. I now I can compile it as C code with such command ocamlc -output-obj -o foo.c foo.ml (I have a situation where I am not alowed to use OCaml compiler to bild my programm for my arcetecture, I can use only specialy modified gcc. so I will compile that programm with sometyhing like gcc -L/usr/lib/ocaml foo.c -lcamlrun -lm -lncurses and Itll run on my archetecture.)

I want to know in general case can a program written in C be faster than one written in OCaml and translated to C?

3
  • I think you want to ask the other way around: can a program written in C be faster than one written in OCaml and translated to C? Obviously a program written in C can be made to run "as fast" as one written in OCaml and translated to C... Apr 15, 2010 at 5:46
  • 2
    There is also an assumption here that the coder's ability to write good / performant code in both languages is identical (I assume a well-written/designed OCaml translated to C may well be much faster than a badly-written/designed C sample) , and conversely the quality of the translation is an unknown factor; it might be truly excellent; it might be diabolical. Apr 15, 2010 at 6:38
  • 2
    OCaml's -output-obj does not produce a C file. It produces an object file that can be linked with C files. The code running is still exactly the same bytecode (for ocamlc) or native code (for ocamlopt) when this option is used, and in fact, in the absence of OCaml -> C translator of any sort, your question is purely theoretical at this time and for the foreseeable future. Apr 15, 2010 at 7:30

2 Answers 2

5

Yes. But all generalisations about such matters are just that, generalisations, and you (and others here) will be able to find counter-examples.

Of course this answer is, itself, a generalisation.

4

Performance is usually not a question of the language, but of the algorithms used to solve the problems. For any problem, there are an absurdillion different algorithms to solve it, with different complexities in time and space.

So you can have a solution with O(n) in ultra-slow language A and a solution with O(n^2) in ultra-fast language B. There will be some threshold n_t for n. A will be slower than B for n < n_t, but faster for n > n_t.

Even when implementing the very same algorithm there are another absurdillion different ways to do it, affecting the constant factor of the complexity. Thus the answer to your question, as already given, is definitely yes, but it doesn't help because the question itself is quite useless.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.