Re: Efficiency and ReplacePart?
- To: mathgroup at smc.vnet.net
- Subject: [mg86167] Re: Efficiency and ReplacePart?
- From: Szabolcs <szhorvat at gmail.com>
- Date: Tue, 4 Mar 2008 02:09:11 -0500 (EST)
- References: <fq3ab7$fmg$1@smc.vnet.net>
On Feb 27, 10:25=A0am, "W. Craig Carter" <ccar... at mit.edu> wrote: > If someone can answer the following question, I will have > learned something about efficiency in mathematica... > > I'd like each second part of a list to a zero-list: > > ftemp = Table[{RandomReal[1, {2}], RandomReal[1, {2}]}, {50000}]; > > Compare: > 1) > Timing[ftemp /. {{a_, b_}, {c_, d_}} :> {{a, b}, {0, 0}}][[1]] > > 2) > Timing[Map[(# = {#[[1]], {0, 0}} &), ftemp]][[1]] Did you mean Timing[Map[{#[[1]], {0, 0}} &, ftemp]][[1]] here? > > 3) Timing[Map[ReplacePart[#, 2 -> {0, 0}] &, ftemp]][[1]] > > (* > or if you like: > times[n_] := > =A0 Module[{ftemp = > =A0 =A0 =A0Table[{RandomReal[1, {2}], RandomReal[1, {2}]}, {n}]}, > =A0 =A0{Timing[ftemp /. {{a_, b_}, {c_, d_}} :> {{a, b}, {0, > 0}}][[1]], > =A0 =A0 Timing[Map[(# = {#[[1]], {0, 0}} &), ftemp]][[1]]}] > > ListPlot[Table[times[i], {i, 100, 10000, 100}]] > *) > > 1 is faster than 2 is faster than 3. Why? I don't think that there is anyone who could look at these 3 programs and predict their performance without running them. Internal implementations change with different versions: in v5.2, 1 is more than twice as slow as 2 on my machine. But a different machine (CPU) may behave in a different way. Look at the benchmark reports included with Mathematica: << Benchmarking` BenchmarkReport @@ $BenchmarkSystems The timing ratios between systems A and B for different tests may be very different. Also, with a list of length 50000, these tests finish very quickly on my computer (below 1 second). Based on my previous experience, I am reluctant to attribute any significance to these timings ... at least in the case when the tests were run on a Windows computer. Try pushing up the timing to at least several seconds, and see if the results change. You may be surprised.