Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question about float refs.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dmitry Bely <dmitry.bely@g...>
Subject: Re: [Caml-list] Re: Question about float refs.
On Mon, Aug 23, 2010 at 4:42 PM, Sylvain Le Gall <sylvain@le-gall.net> wrote:
> On 23-08-2010, Ethan Burns <burns.ethan@gmail.com> wrote:
>> On Mon, Aug 23, 2010 at 8:06 AM, Christophe TROESTLER
>><Christophe.Troestler+ocaml@umh.ac.be> wrote:
>>> On Thu, 19 Aug 2010 07:52:33 -0400, Ethan Burns wrote:
>>>>
>>>> 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
>>>
>>> To add a precision to others' answers : float refs are unboxed
>>> _locally_.  If you rewrite your code as
>>>
>>> let r = ref 0.0 in
>>> for i = 0 to 1000_000_000 do r := float i done;
>>> Printf.printf "%f\n" !r;
>>> Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words
>>>
>>> then it runs at about the same speed as you other version.
>>
>>
>> $ time ./a.out
>> 1000000000.000000
>> words: 2000000367.000000
>>
>> real  0m2.655s
>>
>> It does seem to run a lot faster than my first version, but it also
>> seems to allocate a whole lot.  If it is still allocating just as much
>> why is this version so much faster?
>>
>
> Allocation on the minor heap is very cheap compared to assignement into
> the major heap. It is better to allocate a lot on the minor heap than to
> do operations on the major heap.

Some black magic is needed:

let r = ref 0.0 in
for i = 0 to 1000_000_000 do r := float i done;
Printf.printf "%f\n" (!r +. 0.);
Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words

and the reference will be placed in a register (Caml heap will not be
used inside the loop)

- Dmitry Bely