caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Sylvain Le Gall <sylvain@le-gall.net>
To: caml-list@inria.fr
Subject: Re: Question about float refs.
Date: Mon, 23 Aug 2010 12:42:36 +0000 (UTC)	[thread overview]
Message-ID: <slrni74r5s.e23.sylvain@gallu.homelinux.org> (raw)
In-Reply-To: <AANLkTinCEU3rx-m9cN2MQwmh-EO3-twJw=Ow9W7TKMWW@mail.gmail.com>

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. 

I think the main reason for the difference is that the first example
(float ref not local) implies a call to "caml_modify"
(byterun/memory.c|h) which has a cost. This cost is bigger on amd64
architecture because one test is quite expensive (Is_in_heap I think)
due to address space randomization.

Regards,
Sylvain Le Gall


  reply	other threads:[~2010-08-23 12:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-19 11:52 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 [this message]
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

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=slrni74r5s.e23.sylvain@gallu.homelinux.org \
    --to=sylvain@le-gall.net \
    --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).