This should be easy, but can't find any straight-forward answers on google or SO.
Imagine in R I run a function FOuter()
, and within its body, it does some loop and calls another function FInner()
. Is there a simple way of counting/recording the number of times FInner
gets called? I'm trying to estimate how much time I can save if I optimize FInner
.
Source: R/count-tally.R. count.Rd. count() lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()) . count() is paired with tally() , a lower-level helper that is equivalent to df %>% summarise(n = n()) .
count package:plyr R Documentation Count the number of occurences. Description: Equivalent to 'as. data. frame(table(x))', but does not include combinations with zero counts.
You're looking for trace
.
f1 <- function() 1
f2 <- function() {
for(i in 1:10) f1()
}
.count <- 0
trace(f1, tracer=function() .count <<- .count +1)
f2()
.count
# 10
untrace(f1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With