MathGroup Archive 2006

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

Search the Archive

Re: A Reap Sow question

  • To: mathgroup at
  • Subject: [mg65232] Re: A Reap Sow question
  • From: albert <awnl at>
  • Date: Sun, 19 Mar 2006 03:19:09 -0500 (EST)
  • References: <> <> <dvgsf0$9qe$>
  • Sender: owner-wri-mathgroup at

Hi János,

> So, my situation is similar to Trurl's.  Right now I am appending to
> a list  and as I am adding more and more items to it by using
> AppendTo, I have to pull back some earlier members of the list,
> because they are needed to create the new members of the list.  This
> method works satisfactory for the first 60 - 80 members of the list
> and my G5 duo core constructor - although it is NOTHING compared to
> Trurl - is happy to churn out them in less than a minute.  As soon as
> I have about 110 items, it takes half an hour to put one new item
> into it.  So I was thinking to give up on AppendTo and use Reap Sow
> to collect the list elements, but on the same time I needed to look
> into the list for earlier elements, because I needed them for
> construction.

I'm not sure if I fully understood what you want and I also am not sure
whether my following suggestion will be a solution concerning speed, but
since AppendTo is a known speed-killer it might be worth a try. 

Have you tried to use DownValues instead of a real list? That has some
overhead ,too, but as far as my experience goes, you won't suffer much if
the number of entries gets big, at least not for a few hundert entries. To
find the last entry you might need to store the current max index in an
additional variable.

That is, instead of using:

(* initialization *)
collection = {}

(* append element *)

(* initialization *)
$maxindex = 0

container[$maxindex++] = element

accessing "list" elements is straightforward then:

collection[[i]] will be container[i]


Last[collection] will be container[$maxindex]

of course you could encapsulate all this in some functions that mimic the
regular list interface (e.g. by defining functions part[], appendto[],
first[] and last[]) if it turns out to solve your problem. It might also
well be that there are better ways to do it. Anyway it will be possible to
access previously defined entries in a while loop unlike when using the
Reap-Sow version and my guess is it will be considerably faster than



  • Prev by Date: Re: Unevaluated values of a[[i]]+b[[j]]
  • Next by Date: Re: BinomialDistribution
  • Previous by thread: run time variability
  • Next by thread: Re: A Reap Sow question