From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 1E01CBC6B for ; Wed, 2 Jan 2008 18:26:09 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMBXe0fRVYT1i2dsb2JhbACQEQEBAQgCBiQFkz+EIQ X-IronPort-AV: E=Sophos;i="4.24,235,1196636400"; d="scan'208";a="20849939" Received: from an-out-0708.google.com ([209.85.132.245]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Jan 2008 18:26:08 +0100 Received: by an-out-0708.google.com with SMTP id b15so1489841ana.102 for ; Wed, 02 Jan 2008 09:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; bh=EvOQJz2jKbmFjVueenGC1h15NqPTo1jof5p4lGYOT1E=; b=F9duzGSBPurWPJgXyJVTpdaTyDtXWRsTTS52Lzdvf7tMJCtuhfQsjQJ5/PtFfpaL2Ov6dPBnpTqiRvMSd6wMNTffR3iPWzF7cJ9TLTJ751Qlsq3DWBb3AW2KQMrTV0HB3XeovYuSYIBYkfOS8PYIuWlOGamxDOsZt0YuWU3Fl0s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=iJZbtMR2msT7SfQW9AGQ4bo2tnvhW9UVt0Y4xVtySm2AoSLvFeV/CYPBn9kVrvbD8DhguD8lvhSU8m9AUebiFvWyePWjmvqOu4eIerBkciU9xeFyHdmn1evC3a3YAsDjLtDLs4qVuxuGGCn2V3rN6r6a4uBcbXApRwHr4e3Oojs= Received: by 10.100.202.9 with SMTP id z9mr30720472anf.80.1199294767364; Wed, 02 Jan 2008 09:26:07 -0800 (PST) Received: from ?192.168.0.7? ( [69.152.95.238]) by mx.google.com with ESMTPS id 36sm20940551aga.2.2008.01.02.09.26.04 (version=SSLv3 cipher=RC4-MD5); Wed, 02 Jan 2008 09:26:06 -0800 (PST) Message-ID: <477BC929.4030109@gmail.com> Date: Wed, 02 Jan 2008 11:26:01 -0600 From: Edgar Friendly User-Agent: Thunderbird 2.0.0.9 (X11/20071031) MIME-Version: 1.0 To: Kuba Ober Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] [NEWBIE] is there an in-place map? References: <200801021152.30105.ober.14@osu.edu> In-Reply-To: <200801021152.30105.ober.14@osu.edu> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; blit:01 arrays:01 iteri:01 iteri:01 arrays:01 cheers:01 blit:01 recursive:01 recursion:01 recursive:01 edgar:98 'for':98 wrote:01 rec:01 caml-list:01 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.