caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* AW: AW: [Caml-list] generic Hashtbl.to_array
@ 2006-07-25 15:53 Christoph Bauer
  2006-07-25 16:35 ` Tom
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Bauer @ 2006-07-25 15:53 UTC (permalink / raw)
  To: Brian Hurt, caml-list

[-- Attachment #1: Type: text/plain, Size: 887 bytes --]


 

The dirtiest solution:

let to_array t =
 let a =  Array.make (Hashtbl.length t) (Obj.magic 0)  in
   ignore
     (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0) ; 
     a  
 
 


Does it work correctly for floats? 

 

Looks good for floats. 
 
# let to_array t =
   let a =  Array.make (Hashtbl.length t) (Obj.magic 0)  in
     ignore
       (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0) ;
       a
 
  ;;
val to_array : ('a, 'b) Hashtbl.t -> ('a * 'b) array = <fun>
# let h = Hashtbl.create 0;;
val h : ('_a, '_b) Hashtbl.t = <abstr>
# Hashtbl.add h 1.0 2.0;;
- : unit = ()
# to_array h;;
- : (float * float) array = [|(1., 2.)|]
# Gc.compact ();;
- : unit = ()
#
 
 BTW, the array should store a pointer to a tuple of two floats, so
I thinkt float or ints doesn't matter. I won't use this solution, because
 it isn't better than others. 

 Christoph Bauer 



[-- Attachment #2: Type: text/html, Size: 3244 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: AW: [Caml-list] generic Hashtbl.to_array
  2006-07-25 15:53 AW: AW: [Caml-list] generic Hashtbl.to_array Christoph Bauer
@ 2006-07-25 16:35 ` Tom
  2006-08-15  8:26   ` Stéphane Glondu
  0 siblings, 1 reply; 3+ messages in thread
From: Tom @ 2006-07-25 16:35 UTC (permalink / raw)
  To: Christoph Bauer; +Cc: Brian Hurt, caml-list

[-- Attachment #1: Type: text/plain, Size: 1361 bytes --]

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!

[-- Attachment #2: Type: text/html, Size: 2241 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] generic Hashtbl.to_array
  2006-07-25 16:35 ` Tom
@ 2006-08-15  8:26   ` Stéphane Glondu
  0 siblings, 0 replies; 3+ messages in thread
From: Stéphane Glondu @ 2006-08-15  8:26 UTC (permalink / raw)
  To: caml-list

We shouldn't talk about Obj.magic, but...

Tom a écrit :
> I also corrected my implementation:
> 
> let mgc = Obj.magic 0      <<< So that the function is executed only once.

Does this provide any benefit? It seems to me that Obj.magic is the
(inlined) identity (so basically Obj.magic 0 is compiled directly into
the integer 0).

-- 
Stéphane


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-08-15  8:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-25 15:53 AW: AW: [Caml-list] generic Hashtbl.to_array Christoph Bauer
2006-07-25 16:35 ` Tom
2006-08-15  8:26   ` Stéphane Glondu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).