I'm using profvis to profile my functions in R, but I want to extract specific timings for subfunctions. For example if I run
a = profvis({ dat <- data.frame(
x = rnorm(5e4),
y = rnorm(5e4)
)
plot(x ~ y, data = dat)
m <- lm(x ~ y, data = dat)
abline(m, col = "red")
})
How do I extract the value of the timings for the subfunctions?
2 Answers 2
Extending on User @Roland's answer.
The last syllable of profvis is vis. I suspect it is short for "visualisation". Your question leaves the impression that you are looking for a table. Staying in base R, we can extend the example given in help(summaryRprof) like
Rprof(tmp <- tempfile())
# your example from profvis examples
dat <- data.frame(
x = rnorm(5e4),
y = rnorm(5e4))
plot(x ~ y, data = dat)
m <- lm(x ~ y, data = dat)
abline(m, col = "red")
Rprof(NULL)
r = summaryRprof(tmp)
unlink(tmp)
> r
$by.self
self.time self.pct total.time total.pct
"deparse" 0.08 44.44 0.08 44.44
"plot.xy" 0.08 44.44 0.08 44.44
".External2" 0.02 11.11 0.02 11.11
$by.total
total.time total.pct self.time self.pct
"do.call" 0.18 100.00 0.00 0.00
"plot.default" 0.18 100.00 0.00 0.00
"plot.formula" 0.18 100.00 0.00 0.00
"plot" 0.18 100.00 0.00 0.00
"deparse" 0.08 44.44 0.08 44.44
"plot.xy" 0.08 44.44 0.08 44.44
"deparse1" 0.08 44.44 0.00 0.00
"paste" 0.08 44.44 0.00 0.00
".External2" 0.02 11.11 0.02 11.11
"plot.new" 0.02 11.11 0.00 0.00
$sample.interval
[1] 0.02
$sampling.time
[1] 0.18
You probably want r$by.total.
Comments
If you want to learn about an object, read the documentation or/and use the str function, i.e., str(a).
You can reporduce the timings in the "data" tab of the widget like this:
profdat <- a$x$message$prof
library(dplyr)
profdat |> group_by(label, depth) |> count(label)
## A tibble: 13 ×ばつ 3
## Groups: label, depth [13]
# label depth n
# <chr> <int> <int>
# 1 .External 2 1
# 2 Rprof 2 1
# 3 base::try 1 2
# 4 deparse 4 32
# 5 deparse1 3 32
# 6 grDevices:::png 2 1
# 7 hook 3 1
# 8 localTitle 3 1
# 9 plot.default 2 85
#10 plot.formula 1 85
#11 plot.xy 3 52
#12 profvis 1 1
#13 title 4 1
You need to multiply the n column with 10 to get ms.
However, you could just use the code shown in the example of help("summaryRprof") and use Rprof directly instead of via profvis.