MathGroup Archive 1998

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

Search the Archive

efficiency of Function



This message is a question on the efficiency of Mathematica's Function.

--

The following test shows that Function that uses Slot as its formal
parameters are significantly faster than explicit naming of its formal
parameters. i.e. Function[Slot[1]^2] is faster than Function[{x},x^2].

In[67]:=
n=10000;

In[68]:=
Timing@Do[Function[x,List[x,x]][2],{n}]
Timing@Do[Function[List[x],List[x,x]][2],{n}]
Timing@Do[Function[List[Slot[1],Slot[1]]][2],{n}]

Out[68]=
{1.08333 Second,Null}

Out[69]=
{1.01667 Second,Null}

Out[70]=
{0.416667 Second,Null}

If there are many formal parameters, explicit naming slows it down
significantly.

In[71]:=
n=10000;

In[72]:=
Timing@Do[Function[List[a,b,c,d,e],List[a,b,c,d,e]][1,2,3,4,5],{n}]
Timing@Do[
   
Function[List[Slot[1],Slot[2],Slot[3],Slot[4],Slot[5]]][1,2,3,4,5],{n}]

Out[72]=
{1.96667 Second,Null}

Out[73]=
{0.616667 Second,Null}

This shows that the speed difference stick with the function.

In[74]:=
Clear[n,f1,f2];
n=10000;
f1=Function[List[a,b,c,d,e],List[a,b,c,d,e]];
f2=Function[List[Slot[1],Slot[2],Slot[3],Slot[4],Slot[5]]];

In[75]:=
Timing@Do[f1[1,2,3,4,5],{n}]
Timing@Do[f2[1,2,3,4,5],{n}]

Out[75]=
{2.15 Second,Null}

Out[76]=
{0.666667 Second,Null}


My question is: why the speed difference stick with the function? I
assume that functions with identical behaviors should be identical
internally too. (theoretically, of course)

Is this something we can expect improvement, or are there situations
where f1 and f2 behaves differently? (or are there subtle situations
where constructs using Slot and explicit naming can effect the meaning
of the function?)

 Xah, xah@best.com
 http://www.best.com/~xah/PageTwo_dir/more.html
 "Unix, C, csh, C++, Perl,... passing the torch of brain-damaged
tradition."




  • Prev by Date: RE: Trig Error
  • Next by Date: few easy questions
  • Prev by thread: Re: Problem with EPSTIFF
  • Next by thread: Re: efficiency of Function