Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
AW: AW: [Caml-list] generic Hashtbl.to_array
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-07-25 (16:35)
From: Tom <tom.primozic@g...>
Subject: Re: AW: [Caml-list] generic Hashtbl.to_array
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 ( max_int) ( max_int);

let to_array_1 t =
  let dummy =  Array.init 0 (fun _ -> raise Not_found) in

         (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
     (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0) ;

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!