Efficiency and ReplacePart?

• To: mathgroup at smc.vnet.net
• Subject: [mg85896] Efficiency and ReplacePart?
• From: "W. Craig Carter" <ccarter at mit.edu>
• Date: Wed, 27 Feb 2008 04:23:36 -0500 (EST)

```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]]

3) Timing[Map[ReplacePart[#, 2 -> {0, 0}] &, ftemp]][[1]]

(*
or if you like:
times[n_] :=
Module[{ftemp =
Table[{RandomReal[1, {2}], RandomReal[1, {2}]}, {n}]},
{Timing[ftemp /. {{a_, b_}, {c_, d_}} :> {{a, b}, {0,
0}}][[1]],
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?

Craig

```

• Prev by Date: Re: Plotting Time Series
• Next by Date: Re: Another question on Assuming
• Previous by thread: Re: What is the fastest way to threshold data?
• Next by thread: Re: Efficiency and ReplacePart?