[Shootout-list] Re: Improvements to ocaml Random entry
Yaron Minsky
yminsky@cs.cornell.edu
Thu, 17 Jun 2004 22:11:59 -0400
Actually, on my poor-old 700Mhz machine, it's almost twice as fast for
large n. Here are the timings.
dragonfly: yminsky $ time ./foo1 10000000
41.548068
real 0m1.305s
user 0m1.287s
sys 0m0.003s
dragonfly: yminsky $ time ./foo 10000000
41.548068
real 0m0.713s
user 0m0.696s
sys 0m0.005s
dragonfly: yminsky $
On Thu, 17 Jun 2004 22:07:02 -0400, Yaron Minsky <yminsky@gmail.com> wrote:
>
> I've got a somewhat improved version of the ocaml random number
> generator. it's a bit longer in lines, but only because it's in more
> idiomatic ocaml form. For some reason, the ocaml entries all seem
> artificially low on carriage returns.
>
> Anyway, this is more straightforward and about 15-20% faster (at
> least, when the RNG function is run in a tight loop. I'd think
> everything would get dwarfed by the printf in the shootout.)
>
> let im = 139968
> and ia = 3877
> and ic = 29573
> and first_last = 42
> let n = try int_of_string Sys.argv.(1) with _ -> 1
>
> let rec gen_random max last i =
> let new_last = (last * ia + ic) mod im in
> if i > 1 then gen_random max new_last (i - 1)
> else max *. float_of_int new_last /. float im
>
> let () = Printf.printf "%9f\n" (gen_random 100.0 first_last n)
>