Re: List concatenation speed
- To: mathgroup at smc.vnet.net
- Subject: [mg87581] Re: List concatenation speed
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 14 Apr 2008 05:39:31 -0400 (EDT)
- Organization: University of Bergen
- 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={};
If you set poly = {}, then ClearAll is not necessary here.
> Print[Timing[Do[AppendTo[poly,p],{i,1,n}]]];
Print[] is not necessary here. Do not use the semicolon at the end.
> 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?
>
Yes, there is, but it involves using a slightly different algorithm.
The problem is that you are thinking in a procedural way here: you
assign a value to a variable, and change its value in a loop.
Assignment and appending to lists are slow in Mathematica because they
involve copying the whole data structure. Try to avoid this.
I'd use
In[1]:=
p = Table[x, {50}]; n = 10000;
Table[p, {n}]; // Timing
Out[2]= {0., Null}
which is much-much faster. It is not even necessary to use Join or
Append. If you need the sublists joined together, then use
Join @@ Table[p, {n}]
This will produce a flat list of 'x' symbols. Generally, you can build
up a list of lists first, and Join[] or Flatten[] them in the end.
I hope this helps,
Szabolcs