caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Question about float refs.
@ 2010-08-19 11:52 Ethan Burns
  2010-08-19 13:14 ` [Caml-list] " Lukasz Stafiniak
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Ethan Burns @ 2010-08-19 11:52 UTC (permalink / raw)
  To: caml-list; +Cc: Wheeler Ruml

Hello,

I noticed a strange behavior when using references to floating point
values.  My suspicion, initially, was that the references would not be
boxed because they are the same as a record with a single mutable
field that is of type float.  It seems that OCaml still boxes float
refs.  Consider the following two pieces of code:

let r = ref 0.0 ;;
for i = 0 to 1000000000 do r := float i done;
Printf.printf "%f\n" !r;
Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words
---------------
$ time ./a.out
1000000000.000000
words: 2000000373.000000
10.073 secs

This seems to perform a lot of allocations and it certainly takes a
long time to run.  But, now, if we use a record instead:

type float_ref = { mutable data : float }
let r = { data = 0.0 } ;;
for i = 0 to 1000000000 do r.data <- float i done;
Printf.printf "%f\n" r.data;
Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words
---------------
$ time ./a.out
1000000000.000000
words: 373.000000
0.959 secs

It runs *much* quicker and hardly allocates anything.  My question is:
is there something better than creating a special float_ref type every
time I would like a reference to a floating point number?  Typically I
want a reference to something because it is going to change a lot in a
loop and then be accessed after the loop is complete so it is
undesirable for an allocation to occur each time the reference
changes.

Thanks in advance for any help,

Ethan


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2010-09-01 12:30 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-19 11:52 Question about float refs Ethan Burns
2010-08-19 13:14 ` [Caml-list] " Lukasz Stafiniak
2010-08-19 13:34 ` David House
2010-08-19 13:49   ` Till Varoquaux
2010-08-23 12:06 ` Christophe TROESTLER
2010-08-23 12:14   ` Ethan Burns
2010-08-23 12:42     ` Sylvain Le Gall
2010-08-23 13:00       ` [Caml-list] " Dmitry Bely
2010-08-31 19:41         ` Jon Harrop
2010-09-01  7:18           ` Dmitry Bely
2010-09-01  7:46             ` Christophe TROESTLER
2010-09-01  8:31             ` Fabrice Le Fessant
2010-09-01  9:54               ` Ethan Burns
2010-09-01 12:29                 ` Fabrice Le Fessant

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