@@ -302,18 +302,21 @@ cat("\n")
302302cat(" Test 5: Performance Comparison (6x8 grid)\n " )
303303grid_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 )
310314result_opt <- minimum_path_sum_optimized(grid_perf )
311- opt_time <- as.numeric(Sys.time() - start_time , units = " secs" )
312315
313316cat(" Standard DP result:" , result_std $ min_sum , " \n " )
314317cat(" 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 " )
317320cat(" Results match:" , result_std $ min_sum == result_opt , " \n\n " )
318321
319322# Test 6: Multiple Minimum Paths
0 commit comments