Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Interesting optimization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] Interesting optimization
On Thu, 22 Jul 2004 23:07:03 +0200, Daniel Bünzli
<> wrote:
> I usually try not to be too much obsessed with speed, but I had the
> following interesting experience. While rearanging some checksum code I
> thought that I had rewritten it in a more efficient way. However I
> turned out not to be the case.
> I can boil the example to the following (n.b. loops don't compute
> anything usefull). Basically I rewrote update into update'.

Here's a slight addition to your comparison, which may or may not be
of interest to you.  I added this function:

let update'' c =
  let rec loop c' a =
    if a >= 0
      then loop (c' land 0xff) (succ a)
      else c'
  c := (loop !c 0)

Basically, the same thing as your update, only using a tail-recursive
loop with an argument accumulator instead of using a for loop with a
reference accumulator.  Note that I had to dodge a little on the loop
end condition, since a will never be > max_int, which would be the
normal test for the end of the loop.

While I'll pretty much always prefer the tail-call version (which
avoids references altogether), the fact that the reference has so
little impact when used in the way you described is very nice to know.

using update:
real    0m9.707s
user    0m9.710s
sys     0m0.010s

using update':
real    0m16.172s
user    0m16.140s
sys     0m0.030s

using update'':
real    0m8.321s
user    0m8.320s
sys     0m0.000s


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: