title: slow byte code with tail recursion
body: <p>Inspired by the answer to <a href="https://stackoverflow.com/questions/27652007/how-to-properly-time-an-ocaml-program">this SO question</a> I took the code to check an imperative loop against tail recursion:</p>
  
  <pre><code>let rec nothingfunc i =
    match i with
    | 1000000000 -&gt; 1
    | _ -&gt; nothingfunc (i+1)
  
  let nothingloop1 () =
    let i = ref 0 in
     while !i &lt; 1000000000 do incr i done;
     1
  
  let timeit f v =
    let t1 = Unix.gettimeofday() in
    let _ = f v in
    let t2 =  Unix.gettimeofday() in
      t2 -. t1
  
  let () =
    Printf.printf "recursive function: %g s\n%!" (timeit nothingfunc 0);
    Printf.printf "while loop with ref counter buitin incr: %g s\n%!" (timeit nothingloop1 ());
  </code></pre>
  
  <p>For bytecode and native code the results are</p>
  
  <pre><code>str@s131-intel:~&gt; ./bench_loop
  recursive function: 20.7656 s
  while loop with ref counter buitin incr: 12.0642 s
  str@s131-intel:~&gt; ./bench_loop.opt 
  recursive function: 0.755594 s
  while loop with ref counter buitin incr: 0.753947 s
  </code></pre>
  
  <p>The question is: what is the reason for the big difference 20 to 12  seconds execution time?</p>
  
  <p><strong>Edit, my conclusion:</strong></p>
  
  <p>A function call <code>apply</code> (in byte code) involves a stack size check, possible stack enlargement, and a check for signals. For maximum performance the native code compiler will deliver.</p>
  
  <p>(Side note: asking here on SO because it is search engine friendly.)</p>