Measuring and Benchmarking Code Execution Time in R
Overview
MATLAB or R are clearly programming languages specialized for statistical analysis, but as with any language they are not indifferent to performance. Even if speed is not a strength, one should be able to benchmark. In R you can simply wrap the entire code in system.time({}) to measure the elapsed time.
Example
The following is an implementation of the Sieve of Eratosthenes in R that selects 30 odd numbers less than or equal to $2*10^{5}$, checks whether they are prime, and measures the execution time.
eratosthenes<-function(n){
residue<-2:n
while(n %in% residue){
p<-residue[1]
residue<-residue[as.logical(residue%%p)]
}
return(p)
}
set.seed(150421)
test = sample(2*1:10^5+1,30)
system.time({
for(n in test){
print(paste0("eratosthenes(",n,") = ",eratosthenes(n)))
}
print("계산 끝")
})
The execution result is as follows.

