caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@gmail.com>
To: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Interesting optimization
Date: Thu, 22 Jul 2004 17:40:31 -0400	[thread overview]
Message-ID: <d849ad2a04072214407f8b4f26@mail.gmail.com> (raw)
In-Reply-To: <146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch>

On Thu, 22 Jul 2004 23:07:03 +0200, Daniel Bünzli
<daniel.buenzli@epfl.ch> 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'
  in
  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

John.

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


  reply	other threads:[~2004-07-22 21:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-22 21:07 Daniel Bünzli
2004-07-22 21:40 ` John Prevost [this message]
2004-07-23  0:10   ` Brian Hurt
2004-07-23  1:22     ` John Prevost

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=d849ad2a04072214407f8b4f26@mail.gmail.com \
    --to=j.prevost@gmail.com \
    --cc=caml-list@inria.fr \
    /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).