Beingamanforever

pdist & pdist2 benchmarking script

Jan 27th, 2026
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 3.58 KB | None | 0 0
  1. function bench_blocked ()
  2.   N_vals = [500, 1000, 2000, 3000, 4000];
  3.   P = 10;
  4.   n_runs = 5;
  5.   n_warmup = 2;
  6.  
  7.   res_pdist = zeros (numel (N_vals), 4);
  8.   res_pdist2 = zeros (numel (N_vals), 4);
  9.  
  10.   for idx = 1:numel (N_vals)
  11.     N = N_vals(idx);
  12.     rand ("seed", 12345);
  13.     X = rand (N, P);
  14.     Y = rand (N, P);
  15.  
  16.     t_orig = run_timed (@() pdist_orig (X), n_runs, n_warmup);
  17.     t_blocked = run_timed (@() pdist_blocked (X), n_runs, n_warmup);
  18.     res_pdist(idx, :) = [N, t_orig, t_blocked, t_orig / t_blocked];
  19.  
  20.     t_orig = run_timed (@() pdist2_orig (X, Y), n_runs, n_warmup);
  21.     t_blocked = run_timed (@() pdist2_blocked (X, Y), n_runs, n_warmup);
  22.     res_pdist2(idx, :) = [N, t_orig, t_blocked, t_orig / t_blocked];
  23.   endfor
  24.  
  25.   print_table ("pdist: Original vs Blocked", {"N", "t_orig", "t_blocked", "speedup"}, res_pdist);
  26.   print_table ("pdist2: Original vs Blocked", {"N", "t_orig", "t_blocked", "speedup"}, res_pdist2);
  27.  
  28.   save_csv ("bench_pdist_blocked.csv", {"N", "t_orig", "t_blocked", "speedup"}, res_pdist);
  29.   save_csv ("bench_pdist2_blocked.csv", {"N", "t_orig", "t_blocked", "speedup"}, res_pdist2);
  30.  
  31.   make_plot ("pdist_blocked.png", "pdist: Original vs Blocked", N_vals, ...
  32.              res_pdist(:,2), res_pdist(:,3), "Original", "Blocked");
  33.   make_plot ("pdist2_blocked.png", "pdist2: Original vs Blocked", N_vals, ...
  34.              res_pdist2(:,2), res_pdist2(:,3), "Original", "Blocked");
  35. endfunction
  36.  
  37. function t = run_timed (fn, n_runs, n_warmup)
  38.   for w = 1:n_warmup, fn (); endfor
  39.   times = zeros (n_runs, 1);
  40.   for r = 1:n_runs, tic; fn (); times(r) = toc; endfor
  41.   t = median (times);
  42. endfunction
  43.  
  44. function print_table (title, headers, data)
  45.   printf ("\n%s\n%s\n", title, repmat ("-", 1, 60));
  46.   printf ("%-8s %12s %12s %12s\n", headers{:});
  47.   for i = 1:rows (data)
  48.     printf ("%-8d %12.4f %12.4f %12.2fx\n", data(i,1), data(i,2), data(i,3), data(i,4));
  49.   endfor
  50.   printf ("\n");
  51. endfunction
  52.  
  53. function save_csv (fname, headers, data)
  54.   fid = fopen (fname, "w");
  55.   fprintf (fid, "%s\n", strjoin (headers, ","));
  56.   for i = 1:rows (data)
  57.     fprintf (fid, "%d,%.6f,%.6f,%.4f\n", data(i,1), data(i,2), data(i,3), data(i,4));
  58.   endfor
  59.   fclose (fid);
  60.   printf ("Saved: %s\n", fname);
  61. endfunction
  62.  
  63. function make_plot (fname, ttl, N_vals, t1, t2, leg1, leg2)
  64.   h = figure ("visible", "off");
  65.   semilogy (N_vals, t1, "ro-", "linewidth", 2, "markersize", 8);
  66.   hold on;
  67.   semilogy (N_vals, t2, "bs-", "linewidth", 2, "markersize", 8);
  68.   hold off;
  69.   xlabel ("N"); ylabel ("Time (s)"); title (ttl);
  70.   legend ({leg1, leg2}, "location", "northwest");
  71.   grid on;
  72.   print (h, fname, "-dpng", "-r150");
  73.   close (h);
  74.   printf ("Saved: %s\n", fname);
  75. endfunction
  76.  
  77. function Y = pdist_orig (X)
  78.   n = rows (X);
  79.   order = nchoosek (1:n, 2);
  80.   Y = sqrt (sum ((X(order(:,1),:) - X(order(:,2),:)) .^ 2, 2))';
  81. endfunction
  82.  
  83. function Y = pdist_blocked (X)
  84.   n = rows (X);
  85.   n_pairs = n * (n - 1) / 2;
  86.   Y = zeros (1, n_pairs);
  87.   idx = 1;
  88.   for i = 1:n-1
  89.     j_vals = (i+1):n;
  90.     diff_block = X(i, :) - X(j_vals, :);
  91.     dists = sqrt (sum (diff_block .^ 2, 2))';
  92.     n_j = numel (j_vals);
  93.     Y(idx:idx + n_j - 1) = dists;
  94.     idx = idx + n_j;
  95.   endfor
  96. endfunction
  97.  
  98. function D = pdist2_orig (X, Y)
  99.   [ix, iy] = meshgrid (1:rows (X), 1:rows (Y));
  100.   D = sqrt (sum ((X(ix(:),:) - Y(iy(:),:)) .^ 2, 2));
  101.   D = reshape (D, rows (Y), rows (X))';
  102. endfunction
  103.  
  104. function D = pdist2_blocked (X, Y)
  105.   M = rows (X);
  106.   N = rows (Y);
  107.   D = zeros (M, N);
  108.   for i = 1:M
  109.     diff_ij = X(i, :) - Y;
  110.     D(i, :) = sqrt (sum (diff_ij .^ 2, 2))';
  111.   endfor
  112. endfunction
  113.  
Advertisement
Add Comment
Please, Sign In to add comment