Question: # load packages library ( bench ) # initialize variables x < - 1 : 1 0 0 z < - 1 . 6 #

# load packages
library(bench)
# initialize variables
x <-1:100
z <-1.6
# approach I: loop
multiplication <-
function(x,z){
result <- c()
for (i in 1:length(x)){result <- c(result, x[i]*z)}
return(result)
}
result <- multiplication(x,z)
head(result)
# approach II: "R-style"
result2<- x * z
head(result2)
# comparison
benchmarking <-
mark(
result <- multiplication(x,z),
result2<- x * z,
min_iterations =100
)
benchmarking[,4:9]
plot(benchmarking, type = "boxplot")
Compare the several different implementations of a code chunk both regarding timing and memory usage.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!