MathGroup Archive 2005

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

Search the Archive

Re: Re: compile / optimize

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53821] Re: [mg53770] Re: compile / optimize
  • From: DrBob <drbob at bigfoot.com>
  • Date: Fri, 28 Jan 2005 02:44:21 -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> <200501271041.FAA07265@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

But notice, in the following, the first and second timings for eo:

e = f[12];
Timing[e /. t -> 0.5]
Timing[eo[[1]] /. t -> 0.5]
Timing@First[eo /. t -> 0.5]
Timing[fc[0.5]]
Timing[fco[0.5]]

{0.016000000000000014*Second, 1.6826718654134356}
{0.015000000000000124*Second, 1.6826718654134356}
{0.*Second, 1.6826718654134356}
{0.016000000000000014*Second, 1.6826718654134354}
{0.*Second, 1.6826718654134354}

I think this means eo[[1]]/. t -> 0.5 isn't actually optimized, but First[eo /. t -> 0.5] IS.

Also notice that using Set versus SetDelayed can be important:

eo2 = Experimental`OptimizeExpression[e];
fc2 = Compile[t, Evaluate[e]];
fco2 = Compile[t, Evaluate[eo]];
e = f[12];
Timing[e /. t -> 0.5]
Timing[eo2[[1]] /. t -> 0.5]
Timing@First[eo2 /. t -> 0.5]
Timing[fc2[0.5]]
Timing[fco2[0.5]]

{0.016000000000000014*Second, 1.6826718654134356}
{0.01499999999999968*Second, 1.6826718654134356}
{0.*Second, 1.6826718654134356}
{0.*Second, 1.6826718654134354}
{0.*Second, 1.6826718654134354}

With SetDelayed, the time to Compile was included in timings.

Bobby

On Thu, 27 Jan 2005 05:41:03 -0500 (EST), Drago Ganic <drago.ganic at in2.hr> wrote:

> 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
>>>
>>>
>>
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Algebra of Einstein velocity addition
  • Next by Date: Re: Algebra of Einstein velocity addition
  • Previous by thread: Re: compile / optimize
  • Next by thread: does a matrix equivalent of the Arg function exist?