Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Edgar Friendly <thelema314@gmail.com>
To: Kuba Ober <ober.14@osu.edu>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] [NEWBIE] is there an in-place map?
Date: Wed, 02 Jan 2008 11:26:01 -0600	[thread overview]
Message-ID: <477BC929.4030109@gmail.com> (raw)
In-Reply-To: <200801021152.30105.ober.14@osu.edu>

Kuba Ober wrote:
> I need functionality of map, but done in such a way that the output array
> is given as the argument, not as a return value. The closest I could get was
> 
> let inplace_map f a b = Array.blit (map f a) 0 b 0 (Array.length a)
> 
> Arrays a and b are of the same size.
> This seems very inelegant.
> 
> One could use an adapter function and iteri, but that adds very noticeable 
> overhead, and doesn't seem too elegant either.
> 
> let inplace_map f a b = Array.iteri (fun i src -> b.(i) <- f src; ()) a
> 
> There must be a better way of doing it, right? The given here is that the 
> arrays a and b are there to stay, so we have to use them in-place.
> 
> Cheers, Kuba
> 

let blit_map f a b =
	for i = 0 to (min (Array.length b) (Array.length a)) - 1 do
		b.(i) <- f a.(i);
	done

I don't see any better way to do this.  I can't see any overhead to
using a for loop like this.  I guess the recursive solution should also
work fast:

let blit_map f a b =
	let rec loop i =
		if i < 0 then ()
		else b.(i) <- f a.(i); loop (i-1)
	in
	let max_i = min (Array.length a) (Array.length b) in
	loop (max_i - 1)

But as I've been finding out in trying to performance tune for the
Shootout, 'for' loops seem faster than even tail-recursion.  I'd say
something about function call overhead, but tail recursion shouldn't
have such.  Maybe the work of updating the loop counter in the recursive
case makes the difference...

E.


  parent reply	other threads:[~2008-01-02 17:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-02 16:52 Kuba Ober
2008-01-02 17:00 ` [Caml-list] " Jean-Christophe Filliâtre
2008-01-02 17:06   ` Brian Hurt
2008-01-02 17:26 ` Edgar Friendly [this message]
2008-01-02 17:36   ` Brian Hurt
2008-01-02 19:20     ` Edgar Friendly
2008-01-03 16:01       ` Kuba Ober

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=477BC929.4030109@gmail.com \
    --to=thelema314@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=ober.14@osu.edu \
    /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