RE: AppendTo VERY slow
- To: mathgroup at smc.vnet.net
- Subject: [mg35314] RE: [mg35279] AppendTo VERY slow
- From: "David Park" <djmp at earthlink.net>
- Date: Mon, 8 Jul 2002 03:16:01 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Mike,
In this case, it would have been beneficial to have read the Help for
AppendTo where it says...
"An alternative and often more efficient way to build up a list is to use a
sequence of assignments of the form s = {s, elem}, and then to call Flatten
on the resulting structure. "
Here is an example of the two methods.
outlist = {};
Do[AppendTo[outlist, {1, 2}], {count, 1, 10000}] // Timing
Length[outlist]
{11.43 Second, Null}
10000
Here, instead of adding a List of two elements I add an f[two elements] and
then after Flattening I convert f to List.
outlist = {};
Do[outlist = {outlist, f[1, 2]}, {count, 1, 10000}] // Timing
(outlist = Flatten[outlist] /. f -> List;) // Timing
Length[outlist]
{0.05 Second, Null}
{0. Second, Null}
10000
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> From: Mike [mailto:M.P.Croucher at Sheffield.ac.uk]
To: mathgroup at smc.vnet.net
>
> I use lists a lot in mathematica and tend to use AppendTo[] a lot in
> my programs. Recently I wrote a function that i call over and over
> again and found that the results were coming very slowly and i MEAN
> slowly. I was doing Fourier Transforms and all kinds of stuff so I
> put it down to those at first but I have just put in a load of Print
> statements just after each part of the function to see what was taking
> so long.
>
> I was amazed to see that the Fourier Transforms were so quick and what
> was actually taking the time was a part of my function that collected
> the results togther in the form I wanted and outputted the result. It
> looks like this
>
> Do[
> elem = {xlist[[count]], ylist[[count]]]};
> AppendTo[outlist, elem];
> , {count, 1, number}
> ];
>
> It seems that as the list grows it gets slower and slower. Any tips
> on a way around this would be greatly appreciated (would speed my life
> up no end)
>
>
> Thank
>
> Mike
>