Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] productivity improvement
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: nadji@n...
Subject: Re: [Caml-list] productivity improvement
sebastien FURIC wrote:

> Dave Mason a écrit :
> >
> > Further to my comment on benchmarking...
> >
> > I just ran your ocaml lazy version, and got:
> >
> > real    14m15.124s
> > user    13m26.769s
> > sys     0m2.510s
> >
>

...

> > in garbage collection.  This looks like a perfect garbage-collector
> > stress test!
>

I did some tests and it seems a lot of time is being spent on garbage collection.

> >
> > My guess for why Clean does well suggests a garbage collector better
> > tuned to the problem, but more importantly, a much more efficient
> > handling of laziness.  I suspect you'd see similar results for this
> > problem in Haskell.  Of course that doesn't mean Clean or Haskell will
> > beat OCaml at most, or even many, other benchmarks.  But when laziness
> > is inherent in a solution, expect lazy languages to do much better
> > than eager languages.
> >

... with a lazy style of programming. I ran your test with my computer, and it
took
it around 5mn to complete. I programmed the same test in a functional way
(and with DDR's syntactic sugar), and it now completes in 21s. Moreover,
the program is nearly as short as your Clean one. See below.

>  It is not very surprising to beat O'Caml using a language that is tuned
> to perform lazy computations natively when the problem to solve is lazy!

I don't agree with you when you say that the problem to solve is lazy. The
_algorithm_ you use is lazy. There are certainly lots of ways to solve it
with imperative style, and lots of optimisations (I think of multiprocessor
ones) that you can't implement with your algorithm.


file erat.ml :
(* defines 2 streams : the first one is the functional that I have written,
and the second is Sébastien Furic's one *)
let primes1 =
  let rec erat isprime n =
    if isprime n then [< 'n; erat (fun k -> isprime k && k mod n <> 0) (n + 1) >]

    else erat isprime (n + 1)
  in erat (fun _ -> true) 2

let primes2 =
  let rec filter f = parser
      [< 'n; xs >] -> if f n then [< 'n; filter f xs >] else filter f xs
  in let rec sieve = parser
      [< 'p; xs >] -> [< 'p; sieve (filter (fun x -> x mod p <> 0) xs) >]
  in let rec from k = [< 'k; from (k + 1) >]
  in sieve (from 2)

let _ =
  let primes = match Sys.argv.(1) with "1" -> primes1 | "2" -> primes2
  | _ -> failwith "syntax: erat (1|2) n" in
  let rec print_stream = function
    | 0 -> Printf.printf "\n"
    | n -> Printf.printf "%d " (Stream.next primes); print_stream (n - 1)
  in print_stream (int_of_string (Sys.argv.(2)))

compile it with :
    ocamlopt -o erat -pp camlp4o erat.ml
on my computer,
    time ./erat 1 10000 ->  0:21.74
    time ./erat 2 10000 -> 5:15.87

Cheers,
Nadji

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners