Re: How fast does & perform?
- To: mathgroup at smc.vnet.net
- Subject: [mg107329] Re: How fast does & perform?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Tue, 9 Feb 2010 02:46:24 -0500 (EST)
- References: <hkoic7$t30$1@smc.vnet.net>
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 > > - Kurt > > First, I would say that to do sensible timings, you should choose an example that will run for at least 5 seconds. However, clearly using a pure function is less efficient than one might expect. Pure functions are perhaps best thought of as a convenience feature. Unless a Mathematica program consumes a lot of time, it is usually best to use the syntax that best describes what you want to do. Furthermore, if you do need to improve the performance of your code, it is important first to establish which part of a program is in fact consuming most of the time - usually only a small part. What I am saying, is don't just deprecate one way of doing something in Mathematica just because it is not the fastest, because in most situations it will not matter. David Bailey http://www.dbaileyconsultancy.co.uk