compile / optimize
- To: mathgroup at smc.vnet.net
- Subject: [mg53745] compile / optimize
- From: Frank Brand <frank.brand at t-online.de>
- Date: Wed, 26 Jan 2005 04:36:22 -0500 (EST)
- References: <comgvp$9hg$1@smc.vnet.net> <copavk$ps8$1@smc.vnet.net> <csl1pm$6ve$1@smc.vnet.net> <csnstt$4cr$1@smc.vnet.net> <ct4hka$b1g$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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
>
>