Skip to content

Instantly share code, notes, and snippets.

@omar391
Created May 6, 2026 16:27
Show Gist options
  • Select an option

  • Save omar391/6440c8187a6b7b43a0ec4497f82b0591 to your computer and use it in GitHub Desktop.

Select an option

Save omar391/6440c8187a6b7b43a0ec4497f82b0591 to your computer and use it in GitHub Desktop.
tuneR Grid vs Random Search Mock Benchmark
# Mocking a hyperparameter tuning process
simulate_model_eval <- function(params) {
# Simulate evaluation time (e.g., matrix multiplication)
mat <- matrix(rnorm(100 * 100), nrow = 100)
res <- solve(mat %*% t(mat) + diag(100))
# Return dummy accuracy
return(runif(1, 0.8, 0.95))
}
grid_search <- function(combinations) {
best_acc <- 0
for(i in 1:combinations) {
acc <- simulate_model_eval(i)
if (acc > best_acc) best_acc <- acc
}
return(best_acc)
}
random_search <- function(combinations, n_random) {
best_acc <- 0
for(i in sample(1:combinations, n_random)) {
acc <- simulate_model_eval(i)
if (acc > best_acc) best_acc <- acc
}
return(best_acc)
}
# Run Benchmark
start_time <- Sys.time()
acc_grid <- grid_search(1000)
grid_time <- Sys.time() - start_time
start_time <- Sys.time()
acc_random <- random_search(1000, 250)
random_time <- Sys.time() - start_time
cat(sprintf("Grid Search (1000 evals): %.2f sec, Max Accuracy: %.4f\n", as.numeric(grid_time, units="secs"), acc_grid))
cat(sprintf("Random Search (250 evals): %.2f sec, Max Accuracy: %.4f\n", as.numeric(random_time, units="secs"), acc_random))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment