Version française
Home     About     Download     Resources     Contact us    
Browse thread
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: -- (:)
From: Christoph Bauer <christoph.bauer@l...>
Subject: AW: [Caml-list] generic Hashtbl.to_array
> 
> let to_array_4 t =
>    let init = ref None in
>    begin try Hashtbl.iter (fun k v -> init := Some (k,v); 
> raise Exit) t

My guess: hashtbl has to loop over the first empty buckets.
And this eats the cpu cycles.

>    with Exit -> ()
>    end;
>    match !init with
>    | None -> [| |]
>    | Some i ->
>      let a = Array.make (Hashtbl.length t) i in
>        ignore (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i 
> + 1) t 0);
>        a

Regards,
Christoph Bauer