This vignette of package mcbenchmark (Github, RPubs) documents a multi-core extension to package microbenchmark(Mersmann 2024, v1.5.0).
R terminology might be different from that of mathematics and statistics. Please refer to Appendix Section 4.1 for explanation and reference of the terms and abbreviations used in this vignette.
Sys.info()[c('sysname', 'release', 'machine')]# sysname release machine # "Darwin" "24.6.0" "arm64"R.version# _ # platform aarch64-apple-darwin20 # arch aarch64 # os darwin20 # system aarch64, darwin20 # status # major 4 # minor 5.1 # year 2025 # month 06 # day 13 # svn rev 88306 # language R # version.string R version 4.5.1 (2025-06-13)# nickname Great Square Rootparallel::detectCores()# [1] 8
Experimental (and maybe unstable) features are released extremely frequently to Github. Active developers should use the Github version; suggestions and bug reports are welcome!
Function microbenchmark::microbenchmark() returns a microbenchmark object.
(b =microbenchmark(time =Sys.time(), info =Sys.info(), getenv =Sys.getenv()))# Unit: nanoseconds# expr min lq mean median uq max neval cld# time 533 615 731.03 697 738.0 3936 100 a # info 1968 2132 2652.29 2501 2665.0 15088 100 b # getenv 58589 59245 62913.68 60270 63201.5 113652 100 c
b |>class()# [1] "microbenchmark" "data.frame"
Ratio to Reference Expression
b |>timeratio.microbenchmark()# comparison ratio# 1 info vs. time 3.588235# 2 getenv vs. time 86.470588
b |>timeratio.microbenchmark(ref =2L)# comparison ratio# 1 time vs. info 0.2786885# 2 getenv vs. info 24.0983607
Multi-Core Benchmark
Function mcmicrobenchmark() assigns the benchmark timing to multiple CPU cores with the help of package parallel.
(mb =mcmicrobenchmark(time =Sys.time(), info =Sys.info(), getenv =Sys.getenv()))# Unit: nanoseconds# expr min lq mean median uq max neval cld# time 615 1271.0 4652.712 2275.5 6027.0 42640 104 a # info 139564 207398.5 454819.308 254097.5 370086.5 4826315 104 b # getenv 67896 80339.5 160531.163 159059.5 210145.5 484784 104 c
The mcmicrobenchmark class inherits from microbenchmark class. Therefore all methods defined in package microbenchmark for microbenchmark class are applicable to the mcmicrobenchmark class.
Visualization
The S3 method dispatch microbenchmark:::autoplot.microbenchmark() ignores individual cores.
mb |> microbenchmark:::autoplot.microbenchmark()
The S3 method dispatch autoplot.mcmicrobenchmark() visualize the benchmark by CPU-core.
Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2024. rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.