This document provides some examples of profiling R code using rhosa to estimate polyspectra. Readers will find some hints for understanding the performance of rhosa’s functions.

profile_by_Rprof <- function(...) {
    f <- tempfile()
    Rprof(f)
    (...)
    Rprof(NULL)
    s <- summaryRprof(f)
    unlink(f)
    s
}
sawtooth <- function(r) {
    q <- r/(2*pi)
    q - floor(q) - 0.5
}
x <- three_channel_model(cos, sin, sawtooth)

bicoherence

s <- profile_by_Rprof(bicoherence(x$o1))
s$by.self
#>           self.time self.pct total.time total.pct
#> "d"            1.42    40.34       2.10     59.66
#> ".assert"      0.44    12.50       0.68     19.32
#> "I2"           0.36    10.23       1.52     43.18
#> "vapply"       0.34     9.66       3.50     99.43
#> "FUN"          0.34     9.66       3.14     89.20
#> "I3"           0.34     9.66       1.28     36.36
#> "all"          0.24     6.82       0.24      6.82
#> "mean"         0.02     0.57       3.52    100.00
#> "numeric"      0.02     0.57       0.02      0.57

cross_bicoherence

s <- profile_by_Rprof(with(x, cross_bicoherence(o1, o2, o3)))
s$by.self
#>       self.time self.pct total.time total.pct
#> "FUN"      0.16       80       0.20       100
#> "abs"      0.04       20       0.04        20