MathGroup Archive 2010

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

Search the Archive

Re: Re: How fast does & perform?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107321] Re: [mg107307] Re: How fast does & perform?
  • From: cire g <eric.phys at gmail.com>
  • Date: Tue, 9 Feb 2010 02:44:57 -0500 (EST)
  • References: <hkoic7$t30$1@smc.vnet.net> <201002081255.HAA07568@smc.vnet.net>

Szabolcs Horvát wrote:
> On 2010.02.08. 9:35, 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
>>
>>     
>
> A "&-form" will often (but not always) be faster in large calculations 
> (with Map, etc.) because it can be compiled.  Pattern-based (f[x_] := 
> ...) function can't be compiled.
>
> Also, note the results from timing such fast computations are completely 
> meaningless, for so many reasons ... one of the main (but definitely not 
> the only one) being that the timer is likely to have a low resolution. 
> If you use Windows, your particular examples really mean:
>
> 2.71051*10^-17 seconds and 0. seconds = 0 time unit
> 0.016 seconds = 1 time unit
> 0.031 seconds & 0.032 seconds = 2 time units
>
> Whichever platform you use, don't draw any conclusions from timings 
> unless they're > 10 seconds at least.
>
>
>   
This is what I get (Linux x86_64, version 7) I have to put [[1]] becaue 
timing also put the output..

In[102]:= data=Table[x,{x,(\[Pi]/8),\[Pi],(\[Pi]/(4*1024))}];
In[88]:= g[x_]:=x*Sin[x];

In[103]:= Timing[Table[g[data[[k]]],{k,1,Length[data]}]][[1]]
Out[103]= 0.144978

In[104]:= Timing[Map[g,data]][[1]]
Out[104]= 0.135979

In[106]:= Timing[Map[g[#]&,data]][[1]]
Out[106]= 0.145978

In[108]:= Timing[Map[#*Sin[#]&,data]][[1]]
Out[108]= 0.12998

In[110]:= Timing[#*Sin[#]&/@data][[1]]

Out[110]= 0.138979


  • Prev by Date: Re: Re: Integral confusion
  • Next by Date: Re: Simplifying Bessel functions
  • Previous by thread: Re: How fast does & perform?
  • Next by thread: Re: Re: Re: How fast does & perform?