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

MathGroup Archive 2009

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

Search the Archive

Re: SetDelayed and Evaluate

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104504] Re: [mg104477] SetDelayed and Evaluate
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Sun, 1 Nov 2009 17:58:20 -0500 (EST)
  • References: <200911010902.EAA20123@smc.vnet.net>

Hi,

just check the DownValues and you'll see where you are wrong:

f[z_?NumericQ] := 
  Compile[{{x, _Real}}, Gamma[Sin[x] + 3] + (x - 5)^2][z];
g[z_?NumericQ] := 
  Evaluate[Compile[{{x, _Real}}, Gamma[Sin[x] + 3] + (x - 5)^2]][z];
hh = Compile[{{x, _Real}}, Gamma[Sin[x] + 3] + (x - 5)^2];
h[z_?NumericQ] := hh[z];
DownValues /@ {f, g, h}

and with

Timing[Do[#[x/10.0^4], {x, 10^5}]] & /@ {f, g, h}

you can see more directly how long an evaluation needs since it is not
clear the NMaximize always uses the same (starting values| method) and
therefore calls the function equally often:

{{6.80443, Null}, {6.64842, Null}, {0.504032, Null}}

Cheers
Patrick

On Sun, 2009-11-01 at 04:02 -0500, Peter wrote:
> Hi group,
> 
> after years of experience with Mathematica I've got a simple (?)
> problem:
> 
> When answering to a question on the student support forum (http://
> forums.wolfram.com/student-support/topics/21448/) I tried fo myself
> the folowing variants:
> f[z_?NumericQ]:=Compile[{{x,_Real}},x-x^2][z];
> g[z_?NumericQ]:=Evaluate[Compile[{{x,_Real}},x-x^2]][z];
> and got the following results:
> In[6]:= Timing[Do[NMaximize[f[x],x],{1000}]]
> Out[6]= {35.871,Null}
> In[7]:= Timing[Do[NMaximize[g[x],x],{1000}]]
> Out[7]= {35.962,Null}
> 
> ~1 ms per NMaximize is OK, because there are background-processes
> running.
> 
> But I expect f to compile x-x^2 at every call to f and g to compile
> once on time of definition. Therefore I expected g to be significantly
> faster than f.
> 
> Where am I wrong?
> 
> TIA,
> Peter Pein
> 



  • Prev by Date: Re: SetDelayed and Evaluate
  • Next by Date: Re: SetDelayed and Evaluate
  • Previous by thread: SetDelayed and Evaluate
  • Next by thread: Re: SetDelayed and Evaluate