From lecoutre at stat.ucl.ac.be  Fri Jan 14 17:30:08 2005
From: lecoutre at stat.ucl.ac.be (Eric Lecoutre)
Date: Fri Jan 14 17:38:51 2005
Subject: [Rd] S3/S4 classes performance comparison
In-Reply-To: <Pine.LNX.4.51.0501141609170.30505@artemis.imbe.med.uni-erl
	angen.de>
References: <6.0.1.1.2.20050114142557.031cbdc8@stat4ux.stat.ucl.ac.be>
	<Pine.LNX.4.51.0501141609170.30505@artemis.imbe.med.uni-erlangen.de>
Message-ID: <6.0.1.1.2.20050114172438.032aa578@stat4ux.stat.ucl.ac.be>



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@type <- "vector"
>+    RET@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@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


[Rd] S3/S4 classes performance comparison
Eric Lecoutre