Version française
Home     About     Download     Resources     Contact us    
Browse thread
Strange performances
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Benjamin Canou <benjamin.canou@g...>
Subject: Strange performances
  Hi,

The code following my message is way faster in bytecode than in native
code. Is there a good reason for that or is it a bug ?
Note : It is a (way too, I know) naive implementation of the well known
string suite 1, 11, 21, 1211, 111221, ...

  Benjamin Canou.

=== code ===

let list_of_string s =
  let rec list_of_string s i =
    try s.[i] :: list_of_string s (succ i)
    with _ -> []
  in list_of_string s 0

let rec trans = function
  | '1' :: '1' :: '1' :: tl -> "31" ^ trans tl
  | '1' :: '1' :: tl -> "21" ^ trans tl
  | '1' :: tl -> "11" ^ trans tl
  | '2' :: '2' :: '2' :: tl -> "32" ^ trans tl
  | '2' :: '2' :: tl -> "22" ^ trans tl
  | '2' :: tl -> "12" ^ trans tl
  | '3' :: '3' :: '3' :: tl -> "33" ^ trans tl
  | '3' :: '3' :: tl -> "23" ^ trans tl
  | '3' :: tl -> "13" ^ trans tl
  | [] -> ""
  | _ -> failwith "bad input"

let rec print n s =
  print_endline s ;
  if n > 0 then print (pred n) (trans (list_of_string s))
    
let _ = print 30 "1"

=== perfs ===

benjamin@benjamin-laptop:~/Work/Stuff$ ocamlopt 123.ml -o 123
benjamin@benjamin-laptop:~/Work/Stuff$ time ./123
[...]
real    0m5.245s
user    0m4.944s
sys     0m0.016s
benjamin@benjamin-laptop:~/Work/Stuff$ ocamlc 123.ml -o 123
benjamin@benjamin-laptop:~/Work/Stuff$ time ./123
[...]
real    0m1.097s
user    0m0.840s
sys     0m0.008s
benjamin@benjamin-laptop:~/Work/Stuff$ ocaml -version
The Objective Caml toplevel, version 3.09.2