From: "Daniel Bünzli" <daniel.buenzli@epfl.ch>
To: caml-list@inria.fr
Subject: [Caml-list] Interesting optimization
Date: Thu, 22 Jul 2004 23:07:03 +0200 [thread overview]
Message-ID: <146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch> (raw)
Hello,
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'.
--- test.ml ---
let update c =
let c' = ref !c in
for n = 0 to max_int do
c' := !c' land 0xff
done;
c := !c'
let update' c =
for n = 0 to max_int do
c := !c land 0xff;
done
let compute use_ref =
let x = ref 2 in
if use_ref then update' x else update x;
print_int !x
let main () =
let use_ref = ref false in
let args = [("-ref", (Arg.Set use_ref), "use reference directly")] in
Arg.parse args (fun _ -> ()) "";
compute !use_ref
let () = main ()
---------------
> ocamlopt -o test.opt test.ml
> time ./test.opt
2
real 0m3.500s
user 0m3.230s
sys 0m0.010s
> time ./test.opt -ref
2
real 0m7.599s
user 0m7.550s
sys 0m0.030s
The few that I can read of ppc assembly tells me that in update the
value of c' is directly stored in a register whearas update' accesses
memory on each iteration.
Note that this is not restricted to int's, it occured to me with an
int32. I guess it should work with anything that gets into a register.
Cheers,
Daniel
-------------------
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
next reply other threads:[~2004-07-22 21:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-22 21:07 Daniel Bünzli [this message]
2004-07-22 21:40 ` John Prevost
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=146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch \
--to=daniel.buenzli@epfl.ch \
--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