Rprof

Rprof(filename)
Examine which commands are taking up the most time in an R script or function. I’d call this a crapshoot since Rprof doesn’t typically indicate a particular line of code that is taking a disproportionate amount of time. However, Rprof is easy and quick to implement, so it may serve as a nice first attempt to identify what is making code run slowly.
  • filename – A file name where to store results.

Example. Below the getReturns function from the stockPortfolio is profiled, and we look at a summary of the Rprof info using summaryRprof.
> library(stockPortfolio)
> fileName <- "Rprof_example.txt"
> Rprof(fileName)
> gr <- getReturns(c("GOOG", "MSFT", "IBM"), freq="week")
> Rprof(NULL)
> summaryRprof(fileName)$by.total[1:8,]
                    total.time total.pct self.time
"getReturns"              0.26    100.00       0.0
"strptime"                0.10     38.46       0.1
"as.Date.character"       0.10     38.46       0.0
"as.Date.factor"          0.10     38.46       0.0
"as.Date"                 0.10     38.46       0.0
"charToDate"              0.10     38.46       0.0
"read.delim"              0.10     38.46       0.0
"read.table"              0.10     38.46       0.0
                    self.pct
"getReturns"            0.00
"strptime"             38.46
"as.Date.character"     0.00
"as.Date.factor"        0.00
"as.Date"               0.00
"charToDate"            0.00
"read.delim"            0.00
"read.table"            0.00 

Leave a Reply