daj
daj

Reputation: 7183

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/

Upvotes: 0

Views: 76

Answers (1)

C8H10N4O2
C8H10N4O2

Reputation: 19005

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 

Upvotes: 2

Related Questions