Skip to content
exuber

Replication

Speed

Computation time of radf() across software, and of the R and Python implementations of exuber on the same grid.

This page reports how long radf() takes. The first comparison puts exuber next to the other software that computes the same recursive tests. The second compares the R and the Python implementation of exuber on identical settings.

exuber against other software

The figure reproduces the comparison in Section 4 of Vasilopoulos, Pavlidis and Martinez-Garcia (2022, Journal of Statistical Software). It covers the R packages MultipleBubbles and psymonitor, EViews (rtadf), MATLAB (PSY.m) and Stata, together with exuber as it was benchmarked in the paper (0.4.1) and the current version. Every run uses minw = 30, lag = 1 and a random walk of length n, and each point is the median elapsed time over repeated runs. The vertical axis is on a log scale.

Elapsed time of radf() and of the alternative software, by sample size

All series except exuber 2.0.0 are the archived runs from the paper, so they do not depend on the current implementation. MultipleBubbles and psymonitor are O(T^2) loops written in pure R and already take minutes per run at n = 1000. exuber 2.0.0 is faster than 0.4.1 at every sample size shown. The speed comes from the recursive least-squares algorithm in radf(), which uses the matrix inversion lemma and never inverts a matrix for each window.

R and Python

The R package and pyexuber call the same C++ core (exubercore v0.3.1), so the statistic costs about the same in either language. We timed both on one machine with identical settings: minw = 30, lag = 1, a random walk of length n = 100, 200, ..., 1000, and the median of five block medians of 60 calls each. R is exuber 2.0.0 installed with R CMD INSTALL, since a development load compiles without release optimisation flags. Python is pyexuber 0.1.0 from PyPI.

Elapsed time of radf() in R and in Python, by sample size

nexuber 2.0.0 (R), mspyexuber 0.1.0 (Python), ms
1002.00.9
2003.52.8
3005.95.9
40012.311.5
50017.118.4
60023.130.0
70038.144.8
80045.454.1
90059.757.0
100066.795.6

Neither implementation is consistently faster. Python is quicker at the smallest sizes, where the call overhead is a larger share of the time, and the two cross several times after that. The ranking at n = 1000 changed between repeated runs on this machine (Python took between 49 and 96 ms across three runs, R between 55 and 67 ms), so differences of this size should not be read as a property of either language. The difference that matters for users is outside radf(). The Monte Carlo and bootstrap critical values repeat the call many times, and there the setup of the loop and the number of parallel workers decide the time, not the language.

The figure and the table come from three scripts that you can rerun: speed_r.R times the R package, speed_py.py times pyexuber, and speed_plot.R draws the figure from the two tables they write.

Replication scripts

3 standalone scripts support the claims above, and none depends on another. The R scripts run from the exuber/ package root. The 1 Python script checks that pyexuber reproduces the same numbers.

speed_plot.R 38 lines
# Figure comparing exuber::radf() in R and Python (pyexuber), from the two
# tables written by speed_r.R and speed_py.py. Both use minw = 30, lag = 1 and
# a random walk of length n. The y axis is on a log scale. Run from the
# project root:
#   Rscript docs/replication/speed/speed_plot.R
suppressMessages({
  library(ggplot2)
})

dir <- file.path("docs", "replication", "speed")
tbl <- rbind(
  read.csv(file.path(dir, "speed-r.csv")),
  read.csv(file.path(dir, "speed-py.csv"))
)
# Slowest (top of legend) to fastest (bottom), ranked by the n = 1000 time.
at_max <- tbl[tbl$n == max(tbl$n), ]
tbl$software <- factor(tbl$software, levels = at_max$software[order(-at_max$time_ms)])

p <- ggplot(tbl, aes(n, time_ms, colour = software, linetype = software)) +
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = seq(100, 1000, 100)) +
  scale_y_log10(labels = scales::label_comma()) +
  labs(x = "Sample size", y = "Milliseconds (log scale)", colour = NULL, linetype = NULL) +
  theme_bw() +
  theme(
    legend.position = "inside",
    legend.position.inside = c(0.09, 0.86),
    legend.justification = c(0, 1),
    legend.background = element_rect(fill = scales::alpha("white", 0.8), color = "grey80"),
    legend.key.width = unit(1.4, "lines")
  )

out <- file.path(dir, "speed-r-vs-python.png")
ggsave(out, p, width = 8, height = 5.5, dpi = 120)
cat("written:", out, "\n")
print(reshape(tbl, idvar = "n", timevar = "software", direction = "wide"), digits = 4)
speed_py.py 62 lines
"""Timing of exuber.radf() in Python: median elapsed time per call on a random
walk of length n (median of five block medians), with minw=30 and lag=1, for n = 100, 200, ..., 1000.

This is the same grid and the same settings as speed_r.R, which times the R
package. speed_plot.R draws both series. Run it from the project root with a
pyexuber build that includes the compiled core (``pip install pyexuber``)::

    python docs/replication/speed/speed_py.py
"""

from __future__ import annotations

import csv
import statistics
import time
from importlib.metadata import version
from pathlib import Path

import numpy as np

import exuber

MINW = 30
LAG = 1
SAMPLE_SIZE = range(100, 1001, 100)
TIMES = 60  # calls per block
BLOCKS = 5  # the reported time is the median of the block medians


def median_ms(n: int) -> float:
    """Median time of ``radf()`` in milliseconds on a random walk of length n."""
    rw = np.cumsum(np.random.default_rng(123).standard_normal(n))
    exuber.radf(rw, minw=MINW, lag=LAG)  # warm-up call
    blocks = []
    for _ in range(BLOCKS):
        runs = []
        for _ in range(TIMES):
            t0 = time.perf_counter()
            exuber.radf(rw, minw=MINW, lag=LAG)
            runs.append((time.perf_counter() - t0) * 1e3)
        blocks.append(statistics.median(runs))
    return statistics.median(blocks)


def main() -> None:
    rows = []
    for n in SAMPLE_SIZE:
        t_ms = median_ms(n)
        print(f"n = {n:4d}   pyexuber {version('pyexuber')}   {t_ms:9.3f} ms")
        rows.append((f"pyexuber {version('pyexuber')} (Python)", n, t_ms))

    out = Path("docs/replication/speed/speed-py.csv")
    with out.open("w", newline="") as f:
        w = csv.writer(f)
        w.writerow(["software", "n", "time_ms"])
        w.writerows(rows)
    print("written:", out)


if __name__ == "__main__":
    main()
speed_r.R 40 lines
# Timing of exuber::radf() in R: median elapsed time per call on a random walk
# of length n (median of five block medians), with minw = 30 and lag = 1, for n = 100, 200, ..., 1000.
# speed_py.py runs the same grid with the same settings through pyexuber, and
# speed_plot.R draws both series.
#
# Time the installed package, not devtools::load_all(). A development load
# compiles without the optimisation flags of a release build and would
# understate the speed of R. From the project root:
#   R CMD INSTALL exuber
#   Rscript docs/replication/speed/speed_r.R
suppressMessages({
  library(exuber)
  library(microbenchmark)
})
options(exuber.show_progress = FALSE, exuber.parallel = FALSE)

minw <- 30L
lag <- 1L
sample_size <- seq(100L, 1000L, by = 100L)
times <- 60L # calls per block
blocks <- 5L # the reported time is the median of the block medians

res <- do.call(rbind, lapply(sample_size, function(n) {
  set.seed(123)
  rw <- cumsum(rnorm(n))
  radf(rw, minw = minw, lag = lag) # warm-up call
  t_ms <- median(replicate(blocks, {
    median(microbenchmark(
      radf(rw, minw = minw, lag = lag),
      times = times
    )$time) / 1e6
  }))
  cat(sprintf("n = %4d   R exuber %s   %9.3f ms\n", n, packageVersion("exuber"), t_ms))
  data.frame(software = paste0("exuber ", packageVersion("exuber"), " (R)"), n = n, time_ms = t_ms)
}))

out <- file.path("docs", "replication", "speed", "speed-r.csv")
write.csv(res, out, row.names = FALSE)
cat("written:", out, "\n")