Multi-Core Benchmark

Author

Tingting Zhan

Introduction

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.

Package mcbenchmark Imports packages

Prerequisite

Packages mcbenchmark requires R version 4.5.0 (released 2025-04-11) or higher (macOS, Windows). An Integrated Development Environment (IDE), e.g., RStudio or Positron, is not required, but highly recommended. This vignette is created under R version 4.5.1 (2025-06-13) using packages knitr (Xie 2025, v1.50), quarto (Allaire and Dervieux 2025, v1.5.0 with Quarto v1.7.33) and rmarkdown (Allaire et al. 2024, v2.29).

Environment on author’s computer
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 Root
parallel::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!

remotes::install_github('tingtingzhan/mcbenchmark')

Getting Started

Examples in this vignette require that the search path has

library(mcbenchmark)
# Loading required package: microbenchmark
library(ggplot2)

Extend microbenchmark

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
mb |> 
  class()
# [1] "mcmicrobenchmark" "microbenchmark"   "data.frame"

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.

mb |> 
  autoplot(colour = core)

Appendix

Terms & Abbreviations

Term / Abbreviation Description
CRAN, R The Comprehensive R Archive Network
|> Forward pipe operator introduced since R 4.1.0
$ Extract parts of an object
class Object class
flextable Flexible tables, from package flextable (Gohel and Skintzos 2025)
inherits Class inheritance
mc.cores, mclapply, detectCores Number of CPU cores to use
microbenchmark Sub-millisecond accurate timing of expression evaluation, from package microbenchmark (Mersmann 2024)
S3, generic, methods S3 object oriented system, UseMethod; getS3method; https://adv-r.hadley.nz/s3.html
S4, generic, methods S4 object oriented system, isS4; setClass; getMethod; https://adv-r.hadley.nz/s4.html
search Search path
seed Random number generation seed

References

Allaire, JJ, and Christophe Dervieux. 2025. quarto: R Interface to ’Quarto’ Markdown Publishing System. https://doi.org/10.32614/CRAN.package.quarto.
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.
Gohel, David, and Panagiotis Skintzos. 2025. flextable: Functions for Tabular Reporting. https://doi.org/10.32614/CRAN.package.flextable.
Henry, Lionel, and Hadley Wickham. 2025. rlang: Functions for Base Types and Core r and ’Tidyverse’ Features. https://doi.org/10.32614/CRAN.package.rlang.
Mersmann, Olaf. 2024. microbenchmark: Accurate Timing Functions. https://doi.org/10.32614/CRAN.package.microbenchmark.
Wickham, Hadley. 2016. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
Xie, Yihui. 2025. knitr: A General-Purpose Package for Dynamic Report Generation in R. https://yihui.org/knitr/.