0

I am running a naive bayes classifcation algorithm in R. I want to calculate the execution time of the algorithm. Shall i use

Start_time <- proc.time()
{                
  #execution of naive bayes classifier
}
End_time <- proc_time()
Exec_time = End_time - Start_time

to find the execution time. Is it the right way to calculate the execution time of an algorithm?

IRTFM
  • 258,963
  • 21
  • 364
  • 487

1 Answers1

0

You can use package microbenchmark or rbenchmark to test code.

First define a function:

myfunction <- function(){}

then define how many times should run the function

n <- 10

Here is an example using microbenchmark:

microbenchmark::microbenchmark(myfunction(),times=n) #run your function "n" times

Here is an example using rbenchmark:

rbenchmark::benchmark(myfunction(),replications=n) #run your function "n" times

For both packages you can get the result using operator <-. For example:

microbenchmark::microbenchmark(a<-myfunction(),times=n) # result is in variable "a"
rbenchmark::rbenchmark(a<-myfunction(),times=n) # result is in variable "a"
Manos Papadakis
  • 564
  • 5
  • 17