I'm sorry to say that, but I believe that you results are flawed...
If we look at the code of to_array_1 and to_array_5, there is no possibility that the former was faster... if nothing else, it has an additional if jump each and every loop. I simply couldn't believe your results.
Upon inspecting your code with Toploop, I found out some flaws...
let h () =
let h = Hashtbl.create 100000 in
for i = 0 to 99999 do (* <<< not Hashtbl.length h, as it returns 0 for ampty hashtable *)
Hashtbl.add h (Random.int max_int) (Random.int max_int);
done;
h
let to_array_1 t =
let dummy = Array.init 0 (fun _ -> raise Not_found) in
fst
(Hashtbl.fold
(fun k v (a, i) ->
if i = 0 then
let a = Array.make (Hashtbl.length t) (k, v) in
(a, 1) (* <<<<< Not 0, as it causes no progress *)
else (a.(i) <- (k, v); (a, i + 1)))
t (dummy, 0))
I also corrected my implementation:
let mgc = Obj.magic 0 <<< So that the function is executed only once.
let to_array_5 t =
let a = Array.make (Hashtbl.length t) mgc in
ignore
(Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0) ;
a
I tried to do some benchmarking, but I do not have much time... anyhow, my implementation is faster as far as I tested it.
Believe in your dreams!