Search code examples
roptimizationparenthesesbrackets

Is the Parenthesis -> Bracket optimization for R code still relevant?


There was some discussion a while back about how brackets substituting for parenthesis would speed up R code. Is this still true or have optimizations for parentheses been folded into the R distribution post 3.1?

https://radfordneal.wordpress.com/2010/08/19/speeding-up-parentheses-and-lots-more-in-r/


Solution

  • Seems to still be relevant for me. That doesn't mean I'm losing any sleep over it.

    version
    # platform       x86_64-w64-mingw32          
    # arch           x86_64                      
    # os             mingw32                     
    # system         x86_64, mingw32             
    # status                                     
    # major          3                           
    # minor          2.1                         
    # year           2015                        
    # month          06                          
    # day            18                          
    # svn rev        68531                       
    # language       R                           
    # version.string R version 3.2.1 (2015-06-18)
    # nickname       World-Famous Astronaut    
    
    a <- 5; b <- 1; c <- 4; d <- NULL
    f <- function (n) for (i in 1:n) d <- 1/{a*{b+c}}
    system.time(f(10000000))
    # user  system elapsed 
    # 6.63    0.00    6.64 
    
    a <- 5; b <- 1; c <- 4; d <- NULL
    g <- function (n) for (i in 1:n) d <- 1/(a*(b+c))
    system.time(g(10000000))
    # user  system elapsed 
    # 7.27    0.00    7.29