caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] The invert Benchmark
@ 2002-04-17 18:26 Christophe TROESTLER
  2002-04-17 20:04 ` Xavier Leroy
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Christophe TROESTLER @ 2002-04-17 18:26 UTC (permalink / raw)
  To: O'Caml Mailing List

[-- Attachment #1: Type: Text/Plain, Size: 812 bytes --]

Dear Caml riders,

I found by chance the "The invert Benchmark"
(http://www.lib.uchicago.edu/keith/crisis/benchmarks/invert/).  As you
will notice the Caml code (even compiled) performs poorly.  I guess
part of the problem is due to using Map when Hashtbl is more suited.
So I tried to rewrite the code using Hashtbl (attached to this mail).
What I got some trouble to figure out is how to get a list of the keys
where each of the keys appears only once.  I eventually went the easy
way.  Anybody got better ideas to improve efficiency?  Could a "keys"
function be an interesting addition to Hashtbl???

Another related question that popped up is: how to _efficiently_
implement a join operation (join : string -> string list -> string is
defined by: join c [s1;...;sn] = s1 ^ c ^ ... ^ c ^ sn) ?

Cheers,
ChriS

[-- Attachment #2: Code for "The invert Benchmark" --]
[-- Type: Text/Plain, Size: 857 bytes --]

open Printf

let process_input init_size =
  let table = Hashtbl.create init_size
  and split line = match Str.split (Str.regexp_string "\t") line with
    | [a; b] -> (b, a)
    | _ -> failwith "Bad file format" in
  let rec loop() =
    let line = read_line() in 
    let b, a = split line in (Hashtbl.add table b a;  loop())
  in try
      loop()
    with
      | End_of_file -> table


let get_keys t = 
  let module S = Set.Make (struct type t = string 
				  let compare = compare 
			   end) in
    S.elements(Hashtbl.fold (fun k d l -> S.add k l) t S.empty)

let print table =
  let keys = List.sort compare (get_keys table) in
    List.iter (fun b ->
 		 printf "%s" b;
		 let l = List.sort compare (Hashtbl.find_all table b) in
 		   List.iter (fun s -> printf "\t%s" s) l;
		   print_newline()
	      ) keys

let () =
  print(process_input 2000)

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

end of thread, other threads:[~2002-04-18 21:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-17 18:26 [Caml-list] The invert Benchmark Christophe TROESTLER
2002-04-17 20:04 ` Xavier Leroy
2002-04-17 20:28   ` Nadji.Gauthier
2002-04-18 19:07   ` [Caml-list] " Christophe TROESTLER
2002-04-18 19:21     ` Mattias Waldau
2002-04-17 20:13 ` [Caml-list] " Remi VANICAT
2002-04-17 20:30 ` Markus Mottl
2002-04-17 21:19   ` Mattias Waldau
2002-04-18  9:11 ` Jean-Christophe Filliatre

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).