Beingamanforever

linkage benchmarking script

Mar 8th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.72 KB | None | 0 0
  1. ## Deterministic benchmark for linkage.m optimization
  2.  
  3. addpath ("inst");
  4.  
  5. sizes = [50, 100, 200, 400, 600];
  6. methods = {"single", "complete", "average", "weighted"};
  7. num_runs = 5;
  8.  
  9. printf ("=== Linkage Benchmark ===\n\n");
  10.  
  11. results = {};
  12. idx = 1;
  13.  
  14. for n = sizes
  15.   rand ("seed", 12345);
  16.   X = rand (n, 5);
  17.  
  18.   for m = 1:length (methods)
  19.     method = methods{m};
  20.    
  21.     orig_times = zeros (1, num_runs);
  22.     for run = 1:num_runs
  23.       tic;
  24.       Z1 = linkage_original (X, method);
  25.       orig_times(run) = toc;
  26.     endfor
  27.     orig_best = min (orig_times);
  28.    
  29.     opt_times = zeros (1, num_runs);
  30.     for run = 1:num_runs
  31.       tic;
  32.       Z2 = linkage (X, method);
  33.       opt_times(run) = toc;
  34.     endfor
  35.     opt_best = min (opt_times);
  36.    
  37.     err = norm (Z1 - Z2);
  38.     if (err > 1e-12)
  39.       printf ("ERROR: n=%d, %s - diff=%.2e\n", n, method, err);
  40.     endif
  41.    
  42.     speedup = orig_best / opt_best;
  43.     results{idx, 1} = n;
  44.     results{idx, 2} = method;
  45.     results{idx, 3} = orig_best;
  46.     results{idx, 4} = opt_best;
  47.     results{idx, 5} = speedup;
  48.     idx += 1;
  49.    
  50.     printf ("n=%3d, %s: orig=%.4fs, opt=%.4fs, speedup=%.2fx\n", ...
  51.             n, method, orig_best, opt_best, speedup);
  52.   endfor
  53. endfor
  54.  
  55. fid = fopen ("linkage_benchmark_results.csv", "w");
  56. fprintf (fid, "n,method,original,optimized,speedup\n");
  57. for i = 1:size (results, 1)
  58.   fprintf (fid, "%d,%s,%.6f,%.6f,%.2f\n", ...
  59.            results{i,1}, results{i,2}, results{i,3}, results{i,4}, results{i,5});
  60. endfor
  61. fclose (fid);
  62.  
  63. speedups = cell2mat (results(:, 5));
  64. printf ("\n=== Summary ===\n");
  65. printf ("Average speedup: %.2fx\n", mean (speedups));
  66. printf ("Min: %.2fx, Max: %.2fx\n", min (speedups), max (speedups));
  67.  
Advertisement
Add Comment
Please, Sign In to add comment