MathGroup Archive 2011

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

Search the Archive

Re: list manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116392] Re: list manipulation
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 13 Feb 2011 03:06:02 -0500 (EST)

On 2/12/11 at 5:17 AM, ngoble at iwu.edu (Nicholas Goble) wrote:

>You could try a "brute force" method:

>list3={}; For{i=1, i<=Length[list1], i++,
>AppendTo[list3, {list1[[i]], list2[[i]]}]
>];

This is one of the least efficient ways to do combine lists
possible. Consider

In[1]:= a = MemoryInUse[];
list1 = RandomInteger[100, {10000}];
list2 = RandomInteger[100, {10000}];
list3 = {};
b = MemoryInUse[];

In[6]:= For[i = 1, i <= Length[list1], i++,
    AppendTo[list3, {list1[[i]], list2[[i]]}]]; // Timing
c = MemoryInUse[];

Out[6]= {0.636242,Null}

In[8]:= b - a

Out[8]= 87888

In[9]:= c - b

Out[9]= 1079008

versus

In[1]:= a = MemoryInUse[];
list1 = RandomInteger[100, {10000}];
list2 = RandomInteger[100, {10000}];
b = MemoryInUse[];

In[5]:= list3 = Transpose[{list1, list2}]; // Timing
c = MemoryInUse[];

Out[5]= {0.000218,Null}

In[7]:= b - a

Out[7]= 86584

In[8]:= c - b

Out[8]= 516480

Not only is using For and Append much slower, but it consumes
more memory. In part, this is due to Append making a copy of the
entire array when it adds a element at the end.



  • Prev by Date: Re: two questions - Mathematica's statistical capacities
  • Next by Date: Re: how to drop ContextPath when printing from a package?
  • Previous by thread: Re: list manipulation
  • Next by thread: plotting a curve in 3D from data