From: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
To: Damien Doligez <damien.doligez@inria.fr>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] tip for tail recursive map
Date: Mon, 02 Nov 2009 21:00:31 +0100 [thread overview]
Message-ID: <4AEF3A5F.2060306@univ-savoie.fr> (raw)
In-Reply-To: <8F7FB895-BC42-49C6-BAB3-4EDDF761C78B@inria.fr>
[-- Attachment #1.1: Type: text/plain, Size: 3955 bytes --]
>
> You can do better with an ad-hoc encoding of the continuation
> instead of using closures:
>
> let rec map k f = function
> | [] -> List.rev k
> | x :: rl -> map (f x :: k) f rl
> ;;
>
> The memory footprint is smaller, and you spend much less time
> invoking closures.
>
> Note that I haven't bothered benchmarking these two functions.
I did the benchmark with four version of map (below):
List of size 10, 10000000 times with standard map : 0.948059s
List of size 10, 10000000 times with map with rev : 1.800112s
List of size 10, 10000000 times with map with prelist : 3.060192s
List of size 10, 10000000 times with map with obj : 1.704105s
List of size 100, 1000000 times with standard map : 1.068068s
List of size 100, 1000000 times with map with rev : 1.448090s
List of size 100, 1000000 times with map with prelist : 2.668166s
List of size 100, 1000000 times with map with obj : 1.652104s
List of size 1000, 100000 times with standard map : 1.792112s
List of size 1000, 100000 times with map with rev : 2.912182s
List of size 1000, 100000 times with map with prelist : 3.520220s
List of size 1000, 100000 times with map with obj : 2.460154s
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
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 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
standard map = List.map
map with rev = the above code given by Damien Doligez
map with prelist = a dirty map using Obj but through a
not too dirty, but not completely safe interface prelist (attached) :
let pmap f l =
let pl = start () in
let rec fn = function
[] -> Prelist.extract pl
| a::l -> Prelist.cons (f a) pl; fn l
in
fn l
map with obj : a directly dirty obj map :
let objmap f l =
match l with
[] -> []
| x::l' ->
let start = [f x] in
let rec fn current = function
[] -> start
| x::l' ->
let l'' = [f x] in
Obj.set_field (Obj.repr current) 1 (Obj.repr l'');
fn l'' l'
in
fn start l'
Conclusion : dirty map wins for large lists, Standard map wins for small lists,
but if you map a non trivial function (here I map the trivial succ function on int),
there should be no difference so I would suggest to use map with reverse.
The conclusion is that I would replace Xavier's suggestion in another thread :
<< Repeat after me: "Obj.magic is not part of the OCaml language". >>
By
<< Try very very very hard not to use object. If it fails try very hard to use Obj but no C. >>
I still think Obj is safer than C (I do not speak for interfacing with external library where C is
mandatory), but you should be aware that Obj needs as much knowledge about the runtime than
C interface without using the provided C macro ...
Cheers,
Christophe
--
Christophe Raffalli
Universite de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex
tel: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: prelist.ml --]
[-- Type: text/x-ocaml; name="prelist.ml", Size: 486 bytes --]
type 'a prelist = { mutable start : 'a list; mutable current : 'a list}
let start () = { start = []; current = []}
let cons a pl = match pl.current with
[] -> let l = [a] in pl.current <- l; pl.start <- l
| l ->
let l' = [a] in Obj.set_field (Obj.repr l) 1 (Obj.repr l');
pl.current <- l'
let extract pl =
let r = pl.start in
pl.current <- []; pl.start <- [];
(* to guaranty that we can not mute the list once it has been
extracted *)
r
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: prelist.mli --]
[-- Type: text/x-ocaml; name="prelist.mli", Size: 175 bytes --]
type 'a prelist (* mutable type of a list under construction *)
val start : unit -> 'a prelist
val extract : 'a prelist -> 'a list
val cons : 'a -> 'a prelist -> unit
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
next prev parent reply other threads:[~2009-11-02 20:00 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 [this message]
2009-11-02 23:30 ` Jon Harrop
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=4AEF3A5F.2060306@univ-savoie.fr \
--to=christophe.raffalli@univ-savoie.fr \
--cc=caml-list@yquem.inria.fr \
--cc=damien.doligez@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