caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tom <tom.primozic@gmail.com>
To: "Christoph Bauer" <christoph.bauer@lms-gmbh.de>
Cc: "Brian Hurt" <bhurt@janestcapital.com>, caml-list <caml-list@inria.fr>
Subject: Re: AW: [Caml-list] generic Hashtbl.to_array
Date: Tue, 25 Jul 2006 18:35:23 +0200	[thread overview]
Message-ID: <c1490a380607250935nddedda1y3d9d1a420c61c69f@mail.gmail.com> (raw)
In-Reply-To: <26EB47FDD566A7469FC862DAF373792F017112F4@kaiserslautern1.lmsintl.com>

[-- 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 --]

  reply	other threads:[~2006-07-25 16:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-25 15:53 AW: " Christoph Bauer
2006-07-25 16:35 ` Tom [this message]
2006-08-15  8:26   ` Stéphane Glondu
  -- strict thread matches above, loose matches on Subject: below --
2006-07-26 14:41 AW: " Christoph Bauer
2006-07-26  9:29 Christoph Bauer
2006-07-25 15:34 Christoph Bauer
2006-07-25 12:44 Christoph Bauer
2006-07-26  9:46 ` Damien Doligez
2006-07-25 10:19 Christoph Bauer
2006-07-25 10:45 ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c1490a380607250935nddedda1y3d9d1a420c61c69f@mail.gmail.com \
    --to=tom.primozic@gmail.com \
    --cc=bhurt@janestcapital.com \
    --cc=caml-list@inria.fr \
    --cc=christoph.bauer@lms-gmbh.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).