# Function processing time

## Question:

How do I check the processing time of a function's subfunctions in order to optimize it?

But it doesn't show the subfunctions of my function, however it shows many functions that I'm not even using (not that I know of). Is it possible to graph the performances as well?

An example:

``````exemplo = function(x){
res= 0
for(i in 1 : length(x)){
res = res + x[i]
}
print(res)
res_raiz = sqrt(abs(res))
return(res/res_raiz)
}
teste = rnorm(10000)
exemplo(teste)
``````

Your test example is very fast, so profiling will have a hard time showing a lot. Let's generate a larger test vector to make the test take longer:

``````teste = rnorm(10000000)
``````

Basic R profiling can be done with the `Rprof()` function.

``````Rprof()
exemplo(teste)
Rprof(NULL)
summaryRprof()
\$by.self
self.time self.pct total.time total.pct
"exemplo"      4.48    96.55       4.64    100.00
"+"            0.16     3.45       0.16      3.45

\$by.total
total.time total.pct self.time self.pct
"exemplo"       4.64    100.00      4.48    96.55
"+"             0.16      3.45      0.16     3.45

\$sample.interval
[1] 0.02

\$sampling.time
[1] 4.64
``````

With this test size `Rprof()` already shows you the `+` , which is where you spend most of your time (because of the loop).

A useful package for profiling is `profvis` , which also uses `Rprof()` but makes viewing easier. To install use `devtools::install_github("rstudio/profvis")` . In your case you would do:

``````library(profvis)
p <- profvis({exemplo(teste)})
p
``````

And then the same information from `Rprof()` , only in a visual way. The new version of RStudio will already have this integrated.

Scroll to Top