Re: How fast does & perform?
- To: mathgroup at smc.vnet.net
- Subject: [mg107325] Re: How fast does & perform?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 9 Feb 2010 02:45:41 -0500 (EST)
On 2/8/10 at 3:35 AM, canopus56 at yahoo.com (Canopus56) wrote: >Setting aside the syntax question of the thread "What does & mean?", >there appears to be a performance difference between the two syntax >forms. Looks like the := form is faster: >data = Table[x, {x, (\[Pi]/8), \[Pi], (\[Pi]/512)}] >g[x_] := x*Sin[x] >Timing[Table[g[data[[k]]], {k, 1, Length[data]}]] >2.71051*10^-17 seconds >Timing[Map[g, data]] >0. seconds >Timing[Map[g[#] &, data]] >0.016 seconds >Timing[Map[#*Sin[#] &, data]] >0.031 seconds >Timing[#*Sin[#] & /@ data] >0.032 seconds To have any real assurance one way is faster than another, you need longer execution times, at least 1-10 seconds or so to compensate for the resolution limits on timing. Also, it seems when mapping these to a data list the pattern matching syntax is slower. But, consider the following: In[1]:= data = RandomReal[{-\[Pi], \[Pi]}, 10000000]; In[2]:= g[x_] = x Sin[x]; f = # Sin[#] &; In[4]:= Timing[g /@ data;] Out[4]= {14.136,Null} In[5]:= Timing[f /@ data;] Out[5]= {2.00905,Null} In[6]:= Timing[g[data];] Out[6]= {0.487722,Null} In[7]:= Timing[f[data];] Out[7]= {0.487063,Null} Here it is quite apparent avoiding the use of Map, results in much faster execution. And when you take advantage of the fact these function work on lists, it is no longer clear there is any speed advantage to one form or another. Note, I realize the timing data above violates my suggestion for having an execution time of 1-10 seconds for comparison for the cases where Map is not used. But given memory limits in my machine, it really isn't practical to obtain such long execution times for such a simple function.
- Follow-Ups:
- Re: Re: How fast does & perform?
- From: Canopus56 <canopus56@yahoo.com>
- Re: Re: How fast does & perform?