Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Optimizing For loop statement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129884] Re: Optimizing For loop statement
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Fri, 22 Feb 2013 01:06:04 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <kg445l$r5b$1@smc.vnet.net>

On 21/02/2013 03:26, tarpanelli at libero.it wrote:
> Hello, here below you can find a piece of Mathematica code:
> ...
> For[i=3,i<=n,i++,
> s=sigma[a,b,c]
> vol=ReplacePart[vol,{{i,1}->x[[i]],{i,2}->s}];
> Clear[b];
> b=Interpolation[vol];
> ];
>
> where sigma is a module which take as input parameters two scalars (a,c) and one interpolating function (b).
> What I need is to update the data (vol), on which the interpolating function is built, according to the new result of the module sigma which takes, itsself, the interpolating function as input parameter.
>
> I see that the For loop statement is really time-consuming, and I would like to know if it is possible to ptimize it.
> thanks a lot
> Paolo
>
>
People will tell you not to use For because it is slower than using 
functional constructs, but that is not the main problem here. You don't 
want to use ReplacePart to update vol, use something like:

vol[[i]]={x[[i]],s};

Also, you don't need to Clear b before giving it a value! Clearing could 
well be expensive too.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: barchart
  • Next by Date: Re: Mathematica and Lisp
  • Previous by thread: Optimizing For loop statement
  • Next by thread: Is there a way to integrate and differentiate Erfi?