Native OCaml compiler


Is there a reason for OCaml to be using the bytecode compiler (ocamlc). There is a native compiler provided with the distribution that is a lot faster. It is used by calling ocamlopt instead of ocamlc.


Damn right. That in itself sets OCaml apart from most other high-level languages. Could CodinGame consider invoking ocamlopt rather than ocamlc?


Yes please use the native compiler ocamlopt. During contest we have time limit to answer and the native code is something like 10 times faster than bytecode.

Also, please consider linking using the unix library in order to allow acces to timers, gettimeofday, ....


As there don't seem to be any answer to this thread from codingame, I will give the horrible hack that I used recently:

let () =
    if Sys.executable_name <> "/tmp/native" then begin
      ignore (Sys.command "ocamlopt /tmp/ -o /tmp/native");
      ignore (Sys.command "/tmp/native")

Note that this can also solve the problem of linking to str or num.


No since the original code won't compile in bytecode.

For now, I think about use this kind of hack via bash but this might artificially change rankings


Participation in contests needs to use ocamlopt compiler in order to fully use the traditioal 100 ms of processing limit.

So please, use native compiler.


I attach here a code which allows one to translate ocaml code in order to use native compilation.
The code itself and the generated code are uggly but it works Fill free to modify it and to reuse it.

(* translor produce a file which prints the original code into a file before native compile it and then run the obtained code*)

let read_file_and_write fmt cin =
    while true do
      let s = input_line cin in
      Format.fprintf fmt "%s@." (String.escaped s)
  with _ -> ()

let main () =
  let file = Sys.argv.(1) in
  let cin = open_in file in
  let cout = open_out ((Filename.chop_extension (Filename.basename file))^"") in
  let fmt = Format.formatter_of_out_channel cout in 
  Format.fprintf fmt "let s = \"";
  f fmt cin;
  Format.fprintf fmt "\"@.";
  Format.fprintf fmt "let cout = open_out \"\"
                 let _ = output_string cout s
                 let _ = close_out cout
                 let _ = Sys.command \"ocamlopt -unsafe\" 
                 let _ = Sys.command \"./a.out\"
  close_in cin;
  close_out cout;
let _ = main ()


In your solution with a 100 ms limit, you loose the time of compilation. This trick is interesting but not in contest use case when time available is important to win.
I appreciate the fact of sharing this trick.


During last contests, 1 second was allowed to reply in first turn. Compilation is a matter of ms (maybe 10ms) and is only done during fist turn. I agree the trick is not so good but it allows one to use Ocaml in contest when not using the trick would prevent us to use it.

In fact, I've often had more problems with garbage collector, which can take up to 20ms even with native code and can be called at any time (not only during first turn), than with compilation (taking more ore less 1% of first turn time).


Your conclusion : ocaml can be used for competing thanks to a trick.

Replacing in CG parameters or code ocamlc to ocamlopt is a safer approach both for CG platform and for users.

Workarouds are temporary solutions that are remaining forever ....



i'd like not to have to use that quite ugly trick but since, hopefully for now, CG gives us only bytecode compilation .;;;


Good news : ocaml compiler is now ocamlopt (i.e.: the native compiler).

Thank to CG


Yesss !!! Thank you !


Bad news we still can not link with useful libraries like unix in order to handle timeout…