MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: AppendTo VERY slow

  • To: mathgroup at
  • Subject: [mg35302] Re: AppendTo VERY slow
  • From: "Allan Hayes" <hay at>
  • Date: Mon, 8 Jul 2002 03:15:26 -0400 (EDT)
  • References: <ag6ed8$bnm$>
  • Sender: owner-wri-mathgroup at

Append is slow, I'll come back to that later, but more importantly here, you
are thinking too procedurally and doing for yourself what Mathematica's
structural manipulation features do much quicker. In  this case all that you
need is to transpose a matrix:

    number =1000;
    xlist = Table[Random[],{number}];
    ylist = Table[Random[],{number}];

    outlist ={};

        {2.31 Second,Null}

Using Transpose:

    (outlist2= Transpose[{xlist,ylist}]);//Timing

        {0. Second,Null}




If we do have to use Append repeatedly  it is better to use nesting and then
to flatten.
However your example is a bit tricky in that flattening would give just a
list of numbers, but we can get round that by making nest with heads
different from List (here I use the integer 1); then replacing the head  1
with List.

    (outlist3 =1[];
        outlist3 = 1[outlist3,elem],
      outlist3 = List@@Flatten[outlist3]);//Timing

        {0.22 Second,Null}


     outlist=== outlist3


Allan Hayes
Mathematica Training and Consulting
Leicester UK
hay at
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Mike" <M.P.Croucher at> wrote in message
news:ag6ed8$bnm$1 at
> 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

  • Prev by Date: RE: AppendTo VERY slow
  • Next by Date: RE: RE: How to suppress the mesh in ParametricPlot3D ?
  • Previous by thread: RE: AppendTo VERY slow
  • Next by thread: RE: Re: AppendTo VERY slow