Re: How fast does & perform?
- To: mathgroup at smc.vnet.net
- Subject: [mg107307] Re: How fast does & perform?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 8 Feb 2010 07:55:14 -0500 (EST)
- References: <hkoic7$t30$1@smc.vnet.net>
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.
- Follow-Ups:
- Re: Re: How fast does & perform?
- From: cire g <eric.phys@gmail.com>
- Re: Re: How fast does & perform?