From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] tip for tail recursive map
Date: Mon, 2 Nov 2009 23:30:21 +0000 [thread overview]
Message-ID: <200911022330.21302.jon@ffconsultancy.com> (raw)
In-Reply-To: <4AEF3A5F.2060306@univ-savoie.fr>
On Monday 02 November 2009 20:00:31 Christophe Raffalli wrote:
> List of size 10000, 10000 times with standard map : 7.564473s
> List of size 10000, 10000 times with map with rev : 15.452965s
> List of size 10000, 10000 times with map with prelist : 12.672792s
> List of size 10000, 10000 times with map with obj : 11.572724s
Note that standard "map" is still very fast on this list length.
> List of size 100000, 1000 times with standard map : 33.018063s
> List of size 100000, 1000 times with map with rev : 42.142634s
> List of size 100000, 1000 times with map with prelist : 22.161385s
> List of size 100000, 1000 times with map with obj : 20.801299s
Standard map is now relatively slower but only because it is O(n^2). Look at
page 152 figure 7.4 of OCaml for Scientists to see this effect clearly. It is
caused by the periodic traversal of the O(n) deep stack by the GC and it
slows everything down (you get a similar effect with hash tables because the
GC traverses arrays of pointers, like the spine, atomically).
> standard map with size 1000000 segfaults on my machine
> List of size 1000000, 100 times with map with rev : 55.211450s
> List of size 1000000, 100 times with map with prelist : 23.549472s
> List of size 1000000, 100 times with map with obj : 21.777361s
You can use ulimit to get a bigger function call stack and keep running the
ordinary "map" as far as you want.
> Conclusion : dirty map wins for large lists, Standard map wins for small
> lists...
I think you can do a lot better than this and I think Xavier's recommendation
stands (Obj is a horiffically bad idea unless you wrote the compiler and
run-time *and* have the memory of an elephant ;-). Specifically, you just
need to get rid of the O(n^2) behaviour by bounding the stack depth, perhaps
using a trampoline.
IIRC, this was discussed on this list many years ago. One notable observation
was that adding a depth accumulator does not degrade performance. Another
alternative is to convert the list into an array rather than reversing it and
use the array as a kind of alternative to the function call stack (I think F#
does this).
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
prev parent reply other threads:[~2009-11-02 23:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-23 19:55 pikatchou pokemon
2009-11-02 10:33 ` [Caml-list] " Damien Doligez
2009-11-02 19:56 ` Julien Verlaguet
2009-11-02 20:04 ` Christophe Raffalli
2009-11-03 1:29 ` Yaron Minsky
2009-11-02 20:00 ` Christophe Raffalli
2009-11-02 23:30 ` Jon Harrop [this message]
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=200911022330.21302.jon@ffconsultancy.com \
--to=jon@ffconsultancy.com \
--cc=caml-list@yquem.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