Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Tilting at Windmills?

  • To: mathgroup at
  • Subject: [mg62148] Re: [mg62106] Tilting at Windmills?
  • From: gardyloo <gardyloo at>
  • Date: Sat, 12 Nov 2005 03:32:56 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

  Hi, Matt,

    I didn't look to see what large testLists you were working with, or
I'd never have attempted what I'll post here. But my timings aren't too
bad, for a really naive approach. I don't know how the memory usage
compares to yours, but the last line shows a memory usage of just under
3MB (I think).

Hope this helps!



parseList[list_?ListQ]:={{list[[#]], list[[#]]}, {list[[#]], list[[#+1]]}}&/@












{0.232964` Second,Null}


Take[parseList[testList], 10]







"5.2 for Linux (June 20, 2005)"



Matt wrote:

>  Where there's a chance of success, I tend to agonize over details of
>implementation.  Memory usage is one such area.  Here is a statement of
>a problem I was trying to solve in Mathematica:
> Given a list of the following form:{x1,x2,x3,...,xn-1,xn} I want to
>develop an algorithm that will iterate over the input list to produce
>output of the following
>which will then need to be partitioned to end up in the following form:
>which means that if I had a flattened list of length'n' as input, then
>the new flattened list would have a length of 4*(n-2)+2
>Here is my first solution to this problem, along with a test harness
>for validation:
>        lengthOfInList=Length[x];
>        Print["Length of input list: ",lengthOfInList]
>        ];
>      (tempList={tempList,#,#,#,#})&/@x;
>      If[debugOn\[Equal]True,
>        Print["Out list before flattening: ",tempList]
>        ];
>      ret=Delete[Flatten[tempList],{{1},{2},{3},{-3},{-2},{-1}}];
>      If[debugOn\[Equal]True,
>        retLength=Length[ret];
>        Print["Length of out list: ",retLength];
>        Print["Out list length equal to 4(n-2) +
>2?\n",retLength\[Equal]4 (lengthOfInList-2)+2]
>        ];
>      Partition[ret,2]
>      ];
>      Print[createListOfListsForCobwebTypePlot[testList,debugOn]];];
>Although it seems to work just fine, I'm not satisfied with it because
>of this line:
>It seems very bad to me to keep creating a new list of objects as more
>elements are added, i.e. in C or C++ I would allocate the appropriate
>amount of memory up front, and then fill the 'slots'.  One list, one
>memory allocation.  So, I thought to myself about how I might
>'allocate' or 'reserve' memory up front in Mathematica.  What I figured to do,
>was to generate a table object with enough elements (with dummy values)
>up front and then use the object[[n]]=newValue paradigm to replace the
>dummy value with a real value.  That way, there's only one list
>allocated up front.  Because my input list was going to contain
>elements all with a head of Integer, I decided to create the dummy
>table with Integer elements.  If my experiment proved fruitful, I would
>have modified it to contend with other atomic types as appropriate.
>Here is what my modified and 'streamlined' function along with its
>accompanying test harness is:
>      lengthOfInList=Length[x];
>      allocLength=4 lengthOfInList;
>      allocList=Table[11,{allocLength}];
>      If[debugOn\[Equal]True,
>        Print["Length of input list: ",lengthOfInList];
>        Print["Length of allocList: ",allocLength]
>        ];
>      ret=Delete[allocList,{{1},{2},{3},{-3},{-2},{-1}}];
>      If[debugOn\[Equal]True,
>        retLength=Length[ret];
>        Print["Length of out list: ",retLength];
>        Print["Out list length equal to 4(n-2) +
>2?\n",retLength\[Equal]4 (lengthOfInList-2)+2];
>        ];
>      Partition[ret,2]
>      ];
>      Print[createListOfListsForCobwebTypePlotAlt[testList,debugOn]];];
>This produces identical results as the previous solution.  Now, for the
>Timing[Do[createListOfListsForCobwebTypePlot[testList], {1}]][[1]]
>Timing[Do[createListOfListsForCobwebTypePlotAlt[testList], {1}]][[1]]
>With results of
>1.078 Second
>2.109 Second
>much to my chagrin.  You may be wondering why I'm doing this.  The
>reason is that I want to learn and establish proper Mathematica discipline with
>primitive operations such as this, so that I don't have to break bad
>habits later.  I have a feeling the answer is going to be something
>along the lines of:  "There's not much you can do about it, and in fact
>there really is no memory allocation schemes that you can take
>advantage of in Mathematica."  If that's the case, I'm fine with that.  I just
>want to be sure is all.

  • Prev by Date: Re: Tilting at Windmills?
  • Next by Date: Re: Mathematica 1
  • Previous by thread: Tilting at Windmills?
  • Next by thread: Re: Tilting at Windmills?