List concatenation - two more methods, one truly fast
- To: mathgroup at smc.vnet.net
- Subject: [mg87591] List concatenation - two more methods, one truly fast
- From: carlos at Colorado.EDU
- Date: Mon, 14 Apr 2008 05:41:25 -0400 (EDT)
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).