Skip to content

Measuring function execution time in R

sleep_for_a_minute <- function() { Sys.sleep(60) }

start_time <- Sys.time()
sleep_for_a_minute()
end_time <- Sys.time()

end_time - start_time
# Time difference of 1.000327 mins
start.time <- Sys.time()
...Relevent codes...
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
g <- rnorm(100000)
h <- rep(NA, 100000)

# Start the clock!
ptm <- proc.time()

# Loop through the vector, adding one
for (i in 1:100000){
	h[i] <- g[i] + 1
}

# Stop the clock
proc.time() - ptm

   user  system elapsed 
   0.34    0.06    0.41
Published inUncategorized

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *