Re: List concatenation speed
- To: mathgroup at smc.vnet.net
- Subject: [mg87585] Re: List concatenation speed
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 14 Apr 2008 05:40:16 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- 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?
Hi Carlos,
On my system (v6.0.2 Intel 64-bit Mac OS X 1.5.2), using *Sow* and
*Reap* as in
Reap[Do[Sow[p], {i, 1, n}]][[2, 1]]
is more than 80 times faster than your fastest solution. This will not
work on version below 5.xx.
Using *Table* is about 35 faster and works on any version.
Table[p, {i, 1, n}]
Here are the timings for the five approaches:
p = Table[x, {50}]; n = 10000;
ClearAll[poly1]; poly = {};
Print[Timing[poly = Reap[Do[Sow[p], {i, 1, n}]][[2, 1]];]];
ClearAll[poly]; poly = {};
Print[Timing[poly = Table[p, {i, 1, n}];]];
ClearAll[poly]; poly = {};
Print[Timing[Do[poly = Append[poly, p], {i, 1, n}]]];
ClearAll[poly]; poly = {};
Print[Timing[Do[AppendTo[poly, p], {i, 1, n}]]];
ClearAll[poly]; poly = {};
Print[Timing[Do[poly = Join[poly, {p}], {i, 1, n}]]];
{0.00965,Null}
{0.025739,Null}
{0.828765,Null}
{0.848818,Null}
{1.10683,Null}
Best regards,
-- Jean-Marc