## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(dineR)

## ----eval = FALSE-------------------------------------------------------------
# # Check how many cores are available on your machine
# parallel::detectCores()

## -----------------------------------------------------------------------------
# Generate data for the examples below
data <- data_generator(n_X = 150, p = 100, seed = 42)
X <- data$X
Y <- data$Y

## ----eval = FALSE-------------------------------------------------------------
# result_seq <- estimation(X, Y, nlambda = 15, cores = 1)
# result_seq$elapse  # elapsed time in seconds

## ----eval = FALSE-------------------------------------------------------------
# result_par <- estimation(X, Y, nlambda = 15, cores = 4)
# result_par$elapse  # elapsed time in seconds

## ----eval = FALSE-------------------------------------------------------------
# library(dineR)
# 
# run_bench <- function(label, n_X, p, nlambda, cores_par) {
#   data <- data_generator(n_X = n_X, p = p, seed = 42)
#   X <- data$X; Y <- data$Y
# 
#   r_seq <- estimation(X, Y, nlambda = nlambda, cores = 1)
#   r_par <- estimation(X, Y, nlambda = nlambda, cores = cores_par)
# 
#   cat(sprintf(
#     "[%s] seq: %.3fs | par(%d cores): %.3fs | speed-up: %.2fx\n",
#     label, r_seq$elapse, cores_par, r_par$elapse,
#     r_seq$elapse / r_par$elapse
#   ))
# }
# 
# run_bench("Small",    n_X = 100, p = 50,  nlambda = 10, cores_par = 4)
# run_bench("Medium",   n_X = 150, p = 100, nlambda = 15, cores_par = 4)
# run_bench("Large",    n_X = 200, p = 150, nlambda = 20, cores_par = 4)
# run_bench("High-dim", n_X = 100, p = 200, nlambda = 20, cores_par = 4)
# run_bench("Many-lam", n_X = 150, p = 100, nlambda = 30, cores_par = 4)

## ----eval = FALSE-------------------------------------------------------------
# # Recommended cores selection for larger problems
# n_cores <- min(nlambda, parallel::detectCores() - 1)
# result <- estimation(X, Y, nlambda = nlambda, cores = n_cores)

