MathGroup Archive 2006

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

Search the Archive

Re: Efficiency of repeatedly appending to a List

Jon Harrop wrote:
> Andrew Moylan wrote:
>> This caused me to wonder about the efficiency of appending items to
>> Lists in Mathematica in general. Is it an O(N) operation (i.e.,
>> something akin to a realloc in C)? Or does Mathematica employ some sort
>> of anticipatory memory allocation scheme to make adding new elements to
>> lists usually faster than O(N)?
> I think I tested this a while ago and found it to be amortised
> constant-time complexity.

I just tested it again:

l = {};
      First@Timing[Do[AppendTo[l, Random[]], {i, 100}]], {j, 100}] /. 
    Second -> 1]

and I was most certainly wrong, Append is O(n) as Daniel said.

Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists

  • Prev by Date: Too late to upgrade from 4.2 to 5.2....
  • Next by Date: Re: I could do with a list of geometric shape names can anyone help????
  • Previous by thread: Re: Efficiency of repeatedly appending to a List
  • Next by thread: CleanSlate