You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
rm(x) and rm(list="x") are slow. The latter 2-3 times faster, but still very slow (100-200 times slower) compared to a simple assignment, e.g. x <- NULL. For a few number of calls to rm() this makes little difference, but if it's called thousands of times it is noticable.
One reason rm() is slow is that already at the R level it carries lots of extra weight in order to work in many different cases, e.g. rm(x), rm(list="x"), rm(x,y), rm(list=c("x", "y"), envir=env, inherits=TRUE) etc. As the benchmark stats show, calling .Internal(remove("x", ...)) is yet faster, but still 10 times slower than a plain assignment.
>base::rmfunction (..., list=character(), pos=-1, envir= as.environment(pos),
inherits=FALSE)
{
dots<- match.call(expand.dots=FALSE)$...if (length(dots) &&!all(vapply(dots, function(x) is.symbol(x) ||
is.character(x), NA, USE.NAMES=FALSE)))
stop("... must contain names or character strings")
names<- vapply(dots, as.character, "")
if (length(names) ==0L)
names<-character()
list<- .Primitive("c")(list, names)
.Internal(remove(list, envir, inherits))
}
Suggestion 1
As a straightforward first improvement, the base package could provide:
Background
rm(x)
andrm(list="x")
are slow. The latter 2-3 times faster, but still very slow (100-200 times slower) compared to a simple assignment, e.g.x <- NULL
. For a few number of calls torm()
this makes little difference, but if it's called thousands of times it is noticable.Some benchmark results:
Troubleshooting
One reason
rm()
is slow is that already at the R level it carries lots of extra weight in order to work in many different cases, e.g.rm(x)
,rm(list="x")
,rm(x,y)
,rm(list=c("x", "y"), envir=env, inherits=TRUE)
etc. As the benchmark stats show, calling.Internal(remove("x", ...))
is yet faster, but still 10 times slower than a plain assignment.Suggestion 1
As a straightforward first improvement, the
base
package could provide:Suggestion 2
The above could probable be improved by a native implementation. In [1], @s-u suggests:
Miscellaneous
Alternative names for this function:
.rm()
poof()
yank()
See also
The text was updated successfully, but these errors were encountered: