Native OCaml compiler


#1

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.


#2

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


#3

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, ....


#4

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/Answer.ml -o /tmp/native");
      ignore (Sys.command "/tmp/native")
    end

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


#5

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


#6

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.


#7

Hi.
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 =
  try
    while true do
      let s = input_line cin in
      Format.fprintf fmt "%s@." (String.escaped s)
    done
  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))^"_cg.ml") 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 \"test.ml\"
                 let _ = output_string cout s
                 let _ = close_out cout
                 let _ = Sys.command \"ocamlopt -unsafe test.ml\" 
                 let _ = Sys.command \"./a.out\"
                 ";              
  close_in cin;
  close_out cout;
  ()
  
           
let _ = main ()


#8

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.


#9

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).


#10

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 ....


#11

Yes,

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


#12

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

Thank to CG


#13

Yesss !!! Thank you !