This would reduce execution time because of less function lookups, but returns messy code.
cubed <- function(x) {
x * x * x
}
f <- function(n) {
to_cubes <- 0;
for (i in seq_len(n)) {
to_cubes <- to_cubes + cubed(i) # function call (cubed)
}
}
f_opt <- function(n) {
to_cubes <- 0;
for (i in seq_len(n)) {
to_cubes <- to_cubes + (i * i * i) # function inlined
}
}
f_c <- cmpfun(f)
f_opt_c <- cmpfun(f_opt)