can a program written in C be faster than one written in OCaml and translated to C?
So I have some cool Image Processi开发者_StackOverflow中文版ng 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?
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.
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.
精彩评论