[Rd] S3/S4 classes performance comparison

Eric Lecoutre lecoutre at stat.ucl.ac.be
Fri Jan 14 17:30:08 CET 2005



Hi again,

Thanks for that suggestion: I have tested it at higher scale for my 
application and it does indeed increase performance. But not sufficient for 
my application: it still remains a factor of nearly 25!

Anyway, I think I will come back to S3 classes for that project, which will 
involve a lot of different classes (and nested objects).
In some sense, it is a shame: I clearly understand that S4 classes are 
well-founded and can only encourage R developpers to go on with their 
excellent work.
In fact, the problem may be you do too much great work on core R: the 
manipulations with pure S3 classes are so fast!

Best wishes,

Eric



>my experience was that calling the constructor _with_ data is slow, so the
>following performs a little bit better
>
>R> S3content <- function(obj=NULL,add1=NULL,add2=NULL,type="",...){
>+          out <- list(content=obj,add1=add2,add2=add2,type=type)
>+          class(out) <- "S3Content"
>+          return(out)
>+ }
>R>
>R> S3vector <- function(vec,...){
>+    out <- S3content(obj=vec,type="Vector",...)
>+    class(out) <- "S3Vector"
>+    return(out)
>+ }
>R>
>R>
>R> ### S4 classes
>R>
>R>
>setClass("S4content",representation(content="ANY",add1="ANY",add2="ANY",type="character"))
>[1] "S4content"
>R>
>R> S4vector <- function(vec,...){
>+    RET <- new("S4content")
>+    RET at type <- "vector"
>+    RET at content <- vec
>+    RET
>+ }
>R>
>R> test <- rnorm(10000)
>R> gc()
>          used (Mb) gc trigger (Mb)
>Ncells 156181  4.2     350000  9.4
>Vcells  67973  0.6     786432  6.0
>R> system.time(lapply(test,S3vector))
>[1] 0.23 0.00 0.23 0.00 0.00
>R> gc()
>          used (Mb) gc trigger (Mb)
>Ncells 156314  4.2     350000  9.4
>Vcells  68005  0.6     786432  6.0
>R> system.time(lapply(test,S4vector))
>[1] 6.04 0.00 6.04 0.00 0.00
>R>
>
>Torsten

Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte



More information about the R-devel mailing list