[R] R-Core performance question

oehl_list@gmx.de oehl_list at gmx.de
Fri Nov 8 15:05:26 CET 2002


(full replication code and results below)

I observe a sometimes significant performance difference between writing to
a list element via [[<- and $<- . Under RW1.5.1 and RW1.6.1 I find that

  ListOfListOfVector[[SomeName1]][[SomeName2]][SomeInteger]       <-
Something
  ListOfListOfVector[[SomeInteger1]][[SomeInteger2]][SomeInteger]  <-
Something

can be substantially slower than

  ListOfListOfVector$SomeLiteral1$SomeLiteral2[SomeInteger]        <-
Something

Why is that?

The effect depends on listsize and my impression is that the [[<- more often
triggers garbage collection than $<- . 

Interestingly the performance problems of specifying an elementname at
runtime can be reduced by using something like

  eval(substitute(ListOfListOfVector$SomeLiteral1$SomeLiteral2[SomeInteger]
<- Something, list(SomeLiteral1=SomeName1, SomeLiteral2=SomeName2)))

Is it recommended to call eval(substitute()) to do performant assignments?

Thanks for any explanation
Best


Jens Oehlschlägel




t1 <- function(n1=5, n2=5, n3=10){

 cat("preparing ..\n")

 l2 <- 1:n2
 names(l2) <- l2
 l2 <- lapply(l2, function(i)rep(1, n3))

 l1 <- 1:n1
 names(l1) <- l1
 l1 <- lapply(l1, function(i)l2)
 
 cat("measuring ..\n")

 tim <- system.time(
  for (i in 1:n1) 
   for (j in 1:n2)
  {
    l1[[i]][[j]][1] <- 2
    #also slow with l1[[as.character(i)]][[as.character(j)]][1] <- 2
  }
 )[3] / (n1*n2)
 cat("seconds per assignment =", tim, "\n")
 tim
}

s1 <- function(n1=5, n2=5, n3=10){

 cat("preparing ..\n")

 l2 <- 1:n2
 names(l2) <- l2
 l2 <- lapply(l2, function(i)rep(1, n3))

 l1 <- 1:n1
 names(l1) <- l1
 l1 <- lapply(l1, function(i)l2)
 
 cat("measuring ..\n")
 tim <- system.time(
  for (i in 1:n1) 
   for (j in 1:n2)
  {
    eval(substitute(l1$ii$jj[1] <- 2, list(ii=as.character(i),
jj=as.character(j))))
  }
 )[3] / (n1*n2)
 cat("seconds per assignment =", tim, "\n")
 tim
}

t1(n1=10, n2=10, n3=100) / 
s1(n1=10, n2=10, n3=100)

t1(n1=100, n2=100, n3=100) /
s1(n1=100, n2=100, n3=100)

t1(n1=5, n2=5, n3=100000) /
s1(n1=5, n2=5, n3=100000)

t1(n1=10, n2=10, n3=100000) /
s1(n1=10, n2=10, n3=100000)




> version
         _              
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    6.1            
year     2002           
month    11             
day      01             
language R              

> t1(n1=10, n2=10, n3=100) / 
+ s1(n1=10, n2=10, n3=100)
preparing ..
measuring ..
seconds per assignment = 0.0012 
preparing ..
measuring ..
seconds per assignment = 1e-04 
[1] 12
> 
> t1(n1=100, n2=100, n3=100) /
+ s1(n1=100, n2=100, n3=100)
preparing ..
measuring ..
seconds per assignment = 0.000683 
preparing ..
measuring ..
seconds per assignment = 0.000199 
[1] 3.432161
> 
> t1(n1=5, n2=5, n3=100000) /
+ s1(n1=5, n2=5, n3=100000)
preparing ..
measuring ..
seconds per assignment = 0.0908 
preparing ..
measuring ..
seconds per assignment = 0.0052 
[1] 17.46154
> 
> t1(n1=10, n2=10, n3=100000) /
+ s1(n1=10, n2=10, n3=100000)
preparing ..
measuring ..
seconds per assignment = 0.1008 
preparing ..
measuring ..
seconds per assignment = 0.005 
[1] 20.16



> version
         _              
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    5.1            
year     2002           
month    06             
day      17             
language R

> t1(n1=10, n2=10, n3=100) / 
+ s1(n1=10, n2=10, n3=100)
preparing ..
measuring ..
seconds per assignment = 2e-04 
preparing ..
measuring ..
seconds per assignment = 0.001 
[1] 0.2
> 
> t1(n1=100, n2=100, n3=100) /
+ s1(n1=100, n2=100, n3=100)
preparing ..
measuring ..
seconds per assignment = 0.000677 
preparing ..
measuring ..
seconds per assignment = 0.000198 
[1] 3.419192
> 
> t1(n1=5, n2=5, n3=100000) /
+ s1(n1=5, n2=5, n3=100000)
preparing ..
measuring ..
seconds per assignment = 0.0888 
preparing ..
measuring ..
seconds per assignment = 0.0052 
[1] 17.07692
> 
> t1(n1=10, n2=10, n3=100000) /
+ s1(n1=10, n2=10, n3=100000)
preparing ..
measuring ..
seconds per assignment = 0.0991 
preparing ..
measuring ..
seconds per assignment = 0.005 
[1] 19.82




-- 



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list