Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: compile / optimize

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53770] Re: compile / optimize
  • From: "Drago Ganic" <drago.ganic at in2.hr>
  • Date: Thu, 27 Jan 2005 05:41:03 -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> <ct7p0c$f5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
I thought that OptimizeExpression is used automatically by Compile in this 
particular case. Therefore we could just use
   Compile[vars, Evaluate[expr] ]
instead of
   Compile[vars, Evaluate[Experimental`OptimizeExpression[expr]] ]

Here is an example:

f[n_] := Module[{x = 0}, Do[x += Sin[t^2]/(1 + x), {n}]; x]

e = f[2]
Sin[t^2] + Sin[t^2]/(1 + Sin[t^2])

eo := Experimental`OptimizeExpression[e]; eo
Experimental`OptimizedExpression[Block[{$$16, $$17}, $$16 = t^2; $$17 = 
Sin[$$16]; $$17 + $$17/(1 + $$17)]]

fc := Compile[t, Evaluate[e]]; fc
CompiledFunction[{t}, Block[{$$23, $$24}, $$23 = t^2 ; $$24 = Sin[$$23]; 
$$24 + $$24/(1 + $$24)], -CompiledCode-]

fco := Compile[t, Evaluate[eo]]; fco
CompiledFunction[{t}, Block[{$$29, $$30}, $$29 = t^2 ; $$30 = Sin[$$29]; 
$$30 + $$30/(1 + $$30)], -CompiledCode-]

The code looks the same. But, see the timings for n = 12:
e = f[12];


Timing[e /. t -> 0.5]
{0.09 Second, 1.68267}


Timing[eo[[1]] /. t -> 0.5]
{0.091 Second, 1.68267}


Timing[fc[0.5]]
{0.08 Second, 1.68267}


Timing[fco[0.5]]
{0.02 Second, 1.68267}

?!?!? I use $Version 5.0.


Greetings from Croatia,
Drago Ganic


"Frank Brand" <frank.brand at t-online.de> wrote in message 
news:ct7p0c$f5$1 at smc.vnet.net...
> 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
>>
>>
> 



  • Prev by Date: Re: Re: simplifying inside sum, Mathematica 5.1
  • Next by Date: Re: Re: simplifying inside sum, Mathematica 5.1
  • Previous by thread: Re: compile / optimize
  • Next by thread: Re: Re: compile / optimize