Profiling in R can be performed using the following two functions:
Rprof
summaryRprof
Rprof
functionThe Rprof
function records the call stack to the specified file every interval of seconds. It is also used to start and stop the profiling operation.
Rprof(filename = "Rprof.out", append = FALSE, interval = 0.02)
filename
: This is a file to be used for recording the profiling information.append
: This is a boolean field that indicates whether the profiling information is appended to the existing file or be overwritten.interval
: The time elapsed between the two consecutive samples. The default value is 20 milliseconds.summaryRprof
functionThe summaryRprof
function is used to summarize the information given by the Rprof
function.
summaryRprof(filename = "Rprof.out")
filename
: This is a Rprof
function output file.The function returns the following summary statistics:
Metric | Description |
---|---|
by.self | A DataFrame of time spent in the executing function alone |
by.total | A DataFrame of total time spent in the function and its callees |
sample.interval | The sampling time interval (Refer interval parameter of Rprof function) |
sampling.time | The total time spent in profiling the run |
Let’s look at the code below:
Rprof(tmp <- tempfile())n = 15if(n<=0){print("Invalid n value")}else{fibo_list <- numeric(n)fibo_list[1] <- 1fibo_list[2] <- 1print(paste("The first",n," fibonacci numbers are as follows:"))if(n == 1) print(fibo_list[1])else if(n == 2) print(fibo_list)else{for (i in 3:10) fibo_list[i] <- fibo_list[i - 1] + fibo_list[i - 2]print(fibo_list)}}Rprof(NULL)summaryRprof(tmp)
Rprof()
function with a temporary file.n
numbers in the Fibonacci sequence.Rprof(NULL)
.summaryRprof()
function.Free Resources