Skip to content

Commit 1ac9497

Browse files
Sachinn-64Copilot
andauthored
Update dynamic_programming/minimum_path_sum.r
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 568edb0 commit 1ac9497

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

dynamic_programming/minimum_path_sum.r

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,21 @@ cat("\n")
302302
cat("Test 5: Performance Comparison (6x8 grid)\n")
303303
grid_perf <- matrix(sample(1:20, 48, replace = TRUE), nrow = 6, ncol = 8)
304304

305-
start_time <- Sys.time()
306-
result_std <- minimum_path_sum(grid_perf)
307-
std_time <- as.numeric(Sys.time() - start_time, units = "secs")
305+
library(microbenchmark)
306+
307+
mbm <- microbenchmark(
308+
std = minimum_path_sum(grid_perf),
309+
opt = minimum_path_sum_optimized(grid_perf),
310+
times = 100L
311+
)
308312

309-
start_time <- Sys.time()
313+
result_std <- minimum_path_sum(grid_perf)
310314
result_opt <- minimum_path_sum_optimized(grid_perf)
311-
opt_time <- as.numeric(Sys.time() - start_time, units = "secs")
312315

313316
cat("Standard DP result:", result_std$min_sum, "\n")
314317
cat("Optimized DP result:", result_opt, "\n")
315-
cat("Standard DP time:", sprintf("%.4f sec", std_time), "\n")
316-
cat("Optimized DP time:", sprintf("%.4f sec", opt_time), "\n")
318+
cat("Standard DP median time:", sprintf("%.6f sec", median(mbm$time[mbm$expr == "std"])/1e9), "\n")
319+
cat("Optimized DP median time:", sprintf("%.6f sec", median(mbm$time[mbm$expr == "opt"])/1e9), "\n")
317320
cat("Results match:", result_std$min_sum == result_opt, "\n\n")
318321

319322
# Test 6: Multiple Minimum Paths

0 commit comments

Comments
 (0)