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
- Follow-Ups:
- Re: Efficiency and ReplacePart?
- From: Carl Woll <carlw@wolfram.com>
- Re: Efficiency and ReplacePart?