MathGroup Archive 2008

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

Search the Archive

Re: List concatenation speed

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87597] Re: [mg87571] List concatenation speed
  • From: Carl Woll <carlw at wolfram.com>
  • Date: Mon, 14 Apr 2008 05:42:31 -0400 (EDT)
  • References: <200804130732.DAA11515@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?
>  
>
Each of your examples is basically doing an Append operation 10000 
times, and unfortunately, Append is one of the slowest Mathematica 
operations.

For your toy example, simply:

poly = Table[p, {n}];

is practically instantaneous and produces the same answer:

In[901]:= p = Table[x, {50}]; n = 10000;
ClearAll[poly]; poly = {};
Timing[Do[AppendTo[poly, p], {i, 1, n}]]
Timing[poly2 = Table[p, {n}];]
poly === poly2

Out[903]= {4.281,Null}

Out[904]= {0.,Null}

Out[905]= True

For your real application, the best way to do things depends critically 
on what object you are trying to create. If at all possible, you should 
use multipoint, multiline or multipolygon objects to take advantage of 
packed arrays.

Carl Woll
Wolfram Research


  • Prev by Date: Re: waiting for pressed key and reading key
  • Next by Date: Re: Product
  • Previous by thread: List concatenation speed
  • Next by thread: Re: List concatenation speed