Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Efficiency and ReplacePart?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85949] Re: [mg85896] Efficiency and ReplacePart?
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Thu, 28 Feb 2008 02:55:16 -0500 (EST)
  • References: <6285672.1204145042313.JavaMail.root@m08>
  • Reply-to: drmajorbob at longhorns.com

Two more options, one of which seems faster:

n = 500000;
ftemp = Table[{RandomReal[1, {2}], RandomReal[1, {2}]}, {n}];
First@Timing[one = ftemp /. {{a_, b_}, {c_, d_}} :> {{a, b}, {0, 0}}]
First@Timing[two = Map[{#[[1]], {0, 0}} &, ftemp]]
First@Timing[three = Map[ReplacePart[#, 2 -> {0, 0}] &, ftemp]]
First@Timing[four = Map[MapAt[{0, 0} &, #, 2] &, ftemp]]
First@Timing[five = (ftemp[[All, 2]] *= 0; ftemp)]
one == two == three == four == five

0.943361

1.31027

3.20808

1.40886

0.536861

True

Bobby

On Wed, 27 Feb 2008 03:23:36 -0600, W. Craig Carter <ccarter 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]]
>
> 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
>
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Version 6.0.2
  • Next by Date: Re: How to select terms wanted in a series
  • Previous by thread: Re: Efficiency and ReplacePart?
  • Next by thread: Version 6.0.2