<p>I have a function inside a loop inside a function. The inner function acquires and stores a large vector of data in memory (as a global variable... I'm using "R" which is like "S-Plus"). The loop loops through a long list of data to be acquired. The outer function starts the process and passes in the list of datasets to be acquired.</p>

<pre><code>for (dataset in list_of_datasets) {
  for (datachunk in dataset) {
    &lt;process datachunk&gt;
    &lt;store result? as vector? where?&gt;
  }
}
</code></pre>

<p>I programmed the inner function to store each dataset before moving to the next, so all the work of the outer function occurs as side effects on global variables... a big no-no. Is this better or worse than collecting and returning a giant, memory-hogging vector of vectors? Is there a superior third approach?</p>

<p>Would the answer change if I were storing the data vectors in a database rather than in memory? Ideally, I'd like to be able to terminate the function (or have it fail due to network timeouts) without losing all the information processed prior to termination.</p>

[{'answer_id': 79741, 'author': 'Jeffrey', 'author_id': 3259, 'author_profile': 'https://Stackoverflow.com/users/3259', 'pm_score': -1, 'selected': False, 'text': "<p>It's tough to say definitively without knowing the language/compiler used.  However, if you can simply pass a pointer/reference to the object that you're creating, then the size of the object itself has nothing to do with the speed of the function calls.  Manipulating this data down the road could be a different story.</p>\n"}, {'answer_id': 79768, 'author': 'Allen', 'author_id': 6043, 'author_profile': 'https://Stackoverflow.com/users/6043', 'pm_score': 3, 'selected': False, 'text': '<p>use variables in the outer function instead of global variables. This gets you the best of both approaches: you\'re not mutating global state, and you\'re not copying a big wad of data. If you have to exit early, just return the partial results.</p>\n\n<p>(See the "Scope" section in the R manual: <a href="http://cran.r-project.org/doc/manuals/R-intro.html#Scope" rel="noreferrer">http://cran.r-project.org/doc/manuals/R-intro.html#Scope</a>)</p>\n'}, {'answer_id': 79779, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 0, 'selected': False, 'text': "<p>Third approach: inner function returns a reference to the large array, which the next statement inside the loop then dereferences and stores wherever it's needed (ideally with a single pointer store and not by having to memcopy the entire array).</p>\n\n<p>This gets rid of both the side effect and the passing of large datastructures.</p>\n"}, {'answer_id': 79788, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>It's not going to make much difference to memory use, so you might as well make the code clean.</p>\n\n<p>Since R has copy-on-modify for variables, modifying the global object will have the same memory implications as passing something up in return values.</p>\n\n<p>If you store the outputs in a database (or even in a file) you won't have the memory use issues, and the data will be incrementally available as it is created, rather than just at the end.  Whether it's faster with the database depends primarily on how much memory you are using: is the reduction is garbage collection going to pay for the cost of writing to disk.</p>\n\n<p>There are both time and memory profilers in R, so you can see empirically what the impacts are.</p>\n"}, {'answer_id': 79827, 'author': 'leif', 'author_id': 14257, 'author_profile': 'https://Stackoverflow.com/users/14257', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not sure I understand the question, but I have a couple of solutions.</p>\n\n<ol>\n<li><p>Inside the function, create a list of the vectors and return that.</p></li>\n<li><p>Inside the function, create an environment and store all the vectors inside of that.  Just make sure that you return the environment in case of errors.</p></li>\n</ol>\n\n<p>in R:</p>\n\n<pre><code>help(environment)\n\n# You might do something like this:\n\nouter &lt;- function(datasets) {\n  # create the return environment\n  ret.env &lt;- new.env()\n  for(set in dataset) {\n    tmp &lt;- inner(set)\n    # check for errors however you like here.  You might have inner return a list, and\n    # have the list contain an error component\n    assign(set, tmp, envir=ret.env)\n  }\n  return(ret.env)\n}\n\n#The inner function might be defined like this\n\ninner &lt;- function(dataset) {\n  # I don't know what you are doing here, but lets pretend you are reading a data file\n  # that is named by dataset\n  filedata &lt;- read.table(dataset, header=T)\n  return(filedata)\n}\n</code></pre>\n\n<p>leif</p>\n"}, {'answer_id': 79893, 'author': 'Rob Hansen', 'author_id': 14928, 'author_profile': 'https://Stackoverflow.com/users/14928', 'pm_score': 3, 'selected': False, 'text': '<p>Remember your Knuth.  "Premature optimization is the root of all programming evil."</p>\n\n<p>Try the side effect free version.  See if it meets your performance goals.  If it does, great, you don\'t have a problem in the first place; if it doesn\'t, then use the side effects, and make a note for the next programmer that your hand was forced.</p>\n'}, {'answer_id': 86804, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>FYI, here\'s a full sample toy solution that avoids side effects:</p>\n\n<pre><code>outerfunc &lt;- function(names) {\n  templist &lt;- list()\n  for (aname in names) {\n    templist[[aname]] &lt;- innerfunc(aname)\n  }\n  templist\n}\n\ninnerfunc &lt;- function(aname) {\n  retval &lt;- NULL\n  if ("one" %in% aname) retval &lt;- c(1)\n  if ("two" %in% aname) retval &lt;- c(1,2)\n  if ("three" %in% aname) retval &lt;- c(1,2,3)\n  retval\n}\n\nnames &lt;- c("one","two","three")\n\nname_vals &lt;- outerfunc(names)\n\nfor (name in names) assign(name, name_vals[[name]])\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/79709', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']