[R] "privileged slots",

Duncan Murdoch dmurdoch at pair.com
Fri May 28 14:19:14 CEST 2004


On Thu, 27 May 2004 22:55:20 +0200, "Torsten Steuernagel"
<Torsten.Steuernagel at gmx.de> wrote :


>> 2. Is it true
>> that one can gain execution speed by accessing slots using the
>> function slots(object,name,check=FALSE)<-value would speed up
>> execution?
>
>Try this:
>
>> get("@<-")
>function (object, name, value) 
>{
>    arg <- substitute(name)
>    if (is.name(arg)) 
>        name <- as.character(arg)
>    "slot<-"(object, name, TRUE, value)
>}
>
>So "@<-" actually calls "slot<-". Using "slot<-" instead, you save the 
>additional overhead the call to "@<-" introduces. If there will be a real 
>performance gain in replacing "@<-" certainly depends on what you're 
>doing.

I'd advise against doing this kind of optimization.  It will make your
code harder to maintain, and while it might be faster today, if "@<-"
is really a major time sink, it's an obvious candidate for
optimization in R, e.g. by making it .Internal or .Primitive.  When
that happens, your "optimized" code will likely be slower (if it even
works at all).

Duncan Murdoch




More information about the R-help mailing list