MathGroup Archive 2008

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

Search the Archive

Re: List concatenation speed

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87600] Re: List concatenation speed
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Mon, 14 Apr 2008 05:43:05 -0400 (EDT)
  • References: <ftsd1e$bba$1@smc.vnet.net>

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?
> 
The best way is to create a structure like {c,{b,{a,{}}} by operations 
of the form:

poly={p,poly};

When you are done, calculate Reverse[Flatten[poly]] (skip the Reverse if 
the order does not matter or if you can just as easily create the list 
in backward order).

The problem with any form of Append is that it re-builds the whole list 
- which can be very long - each time it is used.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: List concatenation speed
  • Next by Date: Re: Re: Player Pro and Packages
  • Previous by thread: Re: List concatenation speed
  • Next by thread: Re: List concatenation speed