R 代码的括号 -> 括号优化是否仍然相关?
Is the Parenthesis -> Bracket optimization for R code still relevant?
前段时间有人讨论过用方括号代替圆括号会如何加速 R 代码。这仍然是正确的,或者对括号的优化已折叠到 R 分布 post 3.1 中?
https://radfordneal.wordpress.com/2010/08/19/speeding-up-parentheses-and-lots-more-in-r/
似乎对我仍然有意义。这并不意味着我会为此失眠。
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
前段时间有人讨论过用方括号代替圆括号会如何加速 R 代码。这仍然是正确的,或者对括号的优化已折叠到 R 分布 post 3.1 中?
https://radfordneal.wordpress.com/2010/08/19/speeding-up-parentheses-and-lots-more-in-r/
似乎对我仍然有意义。这并不意味着我会为此失眠。
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