MathGroup Archive 2002

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

Search the Archive

Re: AppendTo VERY slow

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35302] Re: AppendTo VERY slow
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 8 Jul 2002 03:15:26 -0400 (EDT)
  • References: <ag6ed8$bnm$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Mike,
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 ={};
    Do[elem={xlist[[count]],ylist[[count]]};
      AppendTo[outlist,elem],
      {count,1,number}];//Timing

        {2.31 Second,Null}

Using Transpose:

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

        {0. Second,Null}

Check

    outlist===outlist2

        True

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[];
      Do[elem={xlist[[count]],ylist[[count]]};
        outlist3 = 1[outlist3,elem],
        {count,1,number}];
      outlist3 = List@@Flatten[outlist3]);//Timing


        {0.22 Second,Null}

Check

     outlist=== outlist3

        True
--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


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




  • 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