Writing functions neatly / efficiently (garbage collection) in R

Excuse my ignorance as I am not a computer engineer but with roots in biology. I've become a great fan of preset objects (a bit of SO and R inferno by Patrick Burns) and would like to improve my coding skills. Instead, I was thinking about writing more efficient functions and had the following question.

Are there any advantages to deleting variables that will be overwritten at the beginning of the next loop, or is it just a waste of time? For the argument, let's say the size of the old and new variables is very similar or identical.

+2


a source to share


2 answers


No. Automatic garbage collection will take care of this just fine.



+1


a source


I think it will really depend on the specifics of the case. In some cases where the object is large it may be a good idea to rm (), especially if it is not needed and there are many other things to do before it gets overwritten. But again, it is impossible to imagine the fact that a strategy can be expensive in terms of computation time.



The only way to know if it's really worth it is to try both and check with system.time ().

+3


a source







All Articles