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.22    35.67       1.82     53.22
#> "vapply"            0.54    15.79       3.32     97.08
#> ".assert"           0.40    11.70       0.60     17.54
#> "I3"                0.36    10.53       1.28     37.43
#> "I2"                0.34     9.94       1.32     38.60
#> "all"               0.20     5.85       0.20      5.85
#> "FUN"               0.18     5.26       2.78     81.29
#> "abs"               0.06     1.75       0.06      1.75
#> "mean.default"      0.06     1.75       0.06      1.75
#> "mean"              0.04     1.17       3.42    100.00
#> "Conj"              0.02     0.58       0.02      0.58

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.14    63.64       0.20     90.91
#> "abs"          0.06    27.27       0.06     27.27
#> "seq.int"      0.02     9.09       0.02      9.09