Re: Another AppendTo replacement problem
- To: mathgroup at smc.vnet.net
 - Subject: [mg118252] Re: Another AppendTo replacement problem
 - From: Ray Koopman <koopman at sfu.ca>
 - Date: Sun, 17 Apr 2011 19:16:08 -0400 (EDT)
 - References: <iobv13$bfp$1@smc.vnet.net> <ioek85$muk$1@smc.vnet.net>
 
On Apr 17, 4:51 am, "Sjoerd C. de Vries" <sjoerd.c.devr... at gmail.com>
wrote:
> The usual trick is to replace AppendTo[list,elem] with list {list,elem}.
See Maxim Rytin, "Two heads are better than one",
http://forums.wolfram.com/mathgroup/archive/2006/May/msg00175.html
The trick he suggests builds just as fast as,
and unravels twice as fast as, the usual trick.
Timing[s = {}; Do[s = {s, h[i,-i] }, {i,1*^6}]; Length@s]
Timing[Length[ss = Flatten@s /. h->List]]
{4.23 Second,2}
{1.82 Second,1000000}
Timing[z = h[]; Do[z = h[z, {i,-i} ], {i,1*^6}]; Length@z]
Timing[Length[zz = List @@ Flatten[z,Infinity,h]]]
{4.25 Second,2}
{0.71 Second,1000000}
zz === ss
True