Re: List concatenation speed
- To: mathgroup at smc.vnet.net
- Subject: [mg87623] Re: [mg87571] List concatenation speed
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Mon, 14 Apr 2008 06:53:45 -0400 (EDT)
- Reply-to: hanlonr at cox.net
p = Table[x, {50}]; n = 10000;
poly1 = {};
Print[Timing[Do[AppendTo[poly1, p], {n}]]];
poly2 = {};
Print[Timing[Do[poly2 = Append[poly2, p], {n}]]];
poly3 = {};
Print[Timing[Do[poly3 = Join[poly3, {p}], {n}]]];
Clear[poly4];
Print[Timing[poly4 = Reap[Table[Sow[p], {n}]][[2, 1]];]];
Clear[poly5];
Print[Timing[poly5 = Table[p, {n}];]];
{3.51951,Null}
{3.50509,Null}
{3.76392,Null}
{0.009156,Null}
{0.001755,Null}
poly1 == poly2 == poly3 == poly4 == poly5
True
If possible structure your problem to use Table or Sow and Reap.
Bob Hanlon
---- carlos at Colorado.EDU wrote:
> I am building mesh plots that require concatenation of thousands of
> Graphics objects into one list for Show[]. This is done by appending
> objects as they are created, and there are several ways to do that.
> Tested 3 of them for speed using a simple object:
>
> p=Table[x,{50}]; n=10000;
> ClearAll[poly]; poly={};
> Print[Timing[Do[AppendTo[poly,p],{i,1,n}]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Append[poly,p],{i,1,n}]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Join[poly,{p}],{i,1,n}]]];
>
> {5.8395 Second,Null}
> {5.7206 Second,Null}
> {6.29728 Second,Null}
>
> Tests were run on version 5.2 on a G5 desktop Mac. I expected Join to
> win,
> but it didnt. Is there a faster way?
>