Re: compile / optimize
- To: mathgroup at smc.vnet.net
- Subject: [mg53792] Re: [mg53745] compile / optimize
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 27 Jan 2005 05:41:35 -0500 (EST)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
f[n_, t_]:=Module[{x=0},Do[x+=Sin[t^2]/(1+x),{n}];x];
f2[n_, t_] := Nest[#+Sin[t^2]/(1+#)&, 0, n];
And@@Table[f[n,t]==f2[n,t],{n,0,12}]
True
Off[Compile::cset];
f3=Compile[{n,t}, Nest[#+Sin[t^2]/(1+#)&, 0, n]];
t0=2*Pi*Random[];
Timing[#[4000,t0]]& /@ {f,f2,f3}
{{0.0799999999999983*Second, 82.8087054652939},
{0.0800000000000125*Second, 82.8087054652939},
{0.009999999999990905*Second, 82.8087054652939}}
Bob Hanlon
>
> From: Frank Brand <frank.brand at t-online.de>
To: mathgroup at smc.vnet.net
> Date: 2005/01/26 Wed AM 04:36:22 EST
> To: mathgroup at smc.vnet.net
> Subject: [mg53792] [mg53745] compile / optimize
>
> Thanks Paul,
>
> but the special point I´m interesting in is if there is a possibility to
> generally optimize and perhaps compile the following function (maximal
> iteration number 12 substituted by n)
>
> f[n_]=Module[{x = 0}, Do[x += Sin[t^2]/(1 + x), {n}]; x]]
>
> ?
>
> Greetings
> Frank
>
>
> Paul Abbott wrote:
> > In article <csnstt$4cr$1 at smc.vnet.net>,
> > "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de> wrote:
> >
> >
> >>ff = Experimental`OptimizeExpression[
> >>Module[{x = 0}, Do[x += Sin[t^2]/(1 + x), {12}]; x]]
> >>
> >>
> >>myfun=Compile[{{t, _Real}}, Evaluate[ff]]
> >
> >
> > However, using OptimizeExpression with Compile does not give any
speed
> > up for the given problem. Compare the following timings:
> >
> > g = Nest[Function[t, (t + x/t)/2], x, 15];
> >
> > f1 = Compile[{{x, _Real}}, Evaluate[g]];
> >
> > First[Timing[vals1 = f1 /@ Range[0.1, 20., 0.001]; ]]
> > 0.11 Second
> >
> > f2 = Compile[{{x, _Real}},
> > Evaluate[Experimental`OptimizeExpression[Evaluate[g]]]];
> >
> > First[Timing[vals2 = f2 /@ Range[0.1, 20., 0.001]; ]]
> > 0.1 Second
> >
> > vals1 == vals2
> > True
> >
> > Cheers,
> > Paul
> >
> >
> >>"Frank Brand" <frank.brand at t-online.de> schrieb im Newsbeitrag
> >>news:csl1pm$6ve$1 at smc.vnet.net...
> >>
> >>>Dear mathgroup members,
> >>>
> >>>can anybody give me an advice how to generally
> >>>
> >>>1.optimize (using the optimization package "optimize.m") and after
that
> >>>2. compile pieces of code like
> >>>
> >>>Module[{t}, t = x; Do[t = (t + x/t)/2, {n}]; t]
> >
> >
> > Note that this code is much clearer as
> >
> > Nest[Function[t, (t + x/t)/2], x, n]
> >
> > And, of course, NewtonIteration is built-in (FindRoot).
> >
> >
> >>>Applying the two-step approach to the code above with a given n
(=15)
> >>>there is a speed up ratio of 8500 compared with the original
exprssion.
> >>>
> >>>Is it possible to apply this procedure to general expressions?
> >>>
> >>>Thanks in advance
> >>>Frank
> >
> >
>
>