Re: List concatenation - two more methods, one truly fast
- To: mathgroup at smc.vnet.net
- Subject: [mg87625] Re: List concatenation - two more methods, one truly fast
- From: Oliver Ruebenkoenig <ruebenko at uni-freiburg.de>
- Date: Mon, 14 Apr 2008 06:54:09 -0400 (EDT)
- References: <ftv8ro$7o1$1@smc.vnet.net>
Carlos,
p[arg_] := Graphics[{RGBColor[Random[], Random[], Random[]],
pp[{{Random[], Random[]}, {Random[], Random[]}, {Random[],
Random[]}}]}]
ClearAll[poly]; poly = {};
Print[Timing[Do[poly = {p[i], poly}, {i, 1, n}];
poly = poly /. pp -> Polygon;
][[1]]];
0.068004 Second
Show[poly]
hth,
Oliver
On Mon, 14 Apr 2008, carlos at colorado.edu wrote:
> As follow up, the following tests are more realistic in that p
> is similar to the Graphics objects produced during a mesh
> plot. The last method (table entry substitution) is about 2 orders
> of magnitude faster. Unfortunately in actual plot generation I
> dont known n (number of objects) in advance, so that one is out.
>
> ClearAll[poly,p,n]; poly={}; n=5000; SetRandom[1234];
> p[arg_]:= {Graphics[RGBColor[Random[],Random[],Random[]]],
> Graphics[Polygon[{{Random[],Random[]},
> {Random[],Random[]},{Random[],Random[]}}]]};
> Print[Timing[Do[AppendTo[poly,p[i]],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Append[poly,p[i]],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Join[poly,{p[i]}],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly={poly,p[i]},{i,1,n}];poly=Flatten[poly]][[1]]];
> ClearAll[poly]; poly=Table[0,{n}];
> Print[Timing[Do[poly[[i]]=p[i],{i,1,n}]][[1]];
>
>
> 11.3361 Second
> 11.3306 Second
> 11.7123 Second
> 7.24559 Second
> 0.061505 Second
>
> Observation: timing of the first 4 methods is roughly O(n^2).
> That would suggest that Mathematica does not implement a true
> list processor in the kernel, since building a list should be O(n).
>
>
>
Oliver Ruebenkoenig, <ruebenko AT uni-freiburg.de>
- Follow-Ups:
- Coordinates of vertices
- From: "King, Peter R" <peter.king@imperial.ac.uk>
- Coordinates of vertices