Beingamanforever

benchmark silhouette.m

Jan 28th, 2026
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.88 KB | None | 0 0
  1. % Benchmark silhouette: Blocked vs Original implementation
  2. addpath('inst');
  3.  
  4. dims = 10; k = 5; trials = 3;
  5. sizes = [5 10 50 100 200 350 500 600 750 900 1000 1250 1500 2000 3000 4000 5000 6000 7000 8000 10000];
  6.  
  7. printf('\n%7s %10s %10s %8s %8s\n', 'N', 'Original', 'Blocked', 'Ratio', 'Winner');
  8. printf('%s\n', repmat('-', 1, 50));
  9.  
  10. results = [];
  11. for n = sizes
  12.   if (n*n*8 > 8e9), printf('%7d  SKIPPED (>8GB)\n', n); continue; endif
  13.  
  14.   rand('seed', 12345); X = rand(n, dims); clust = randi(k, n, 1);
  15.   clusterIDs = unique(clust); m = length(clusterIDs);
  16.  
  17.   % ORIGINAL: pdist + squareform + full matrix loop
  18.   t_orig = [];
  19.   for t = 1:(trials+1)
  20.     tic;
  21.     distMatrix = squareform(pdist(X, 'squaredeuclidean'));
  22.     si_o = zeros(n, 1);
  23.     for i = 1:n
  24.       grp = {}; for j = 1:m, grp{clusterIDs(j)} = distMatrix(i, clust == clusterIDs(j)); endfor
  25.       if length(grp{clust(i)}) == 1, si_o(i) = 1; continue; endif
  26.       a_i = sum(grp{clust(i)}) / (length(grp{clust(i)}) - 1);
  27.       others = clusterIDs(clusterIDs != clust(i));
  28.       b_i = min(arrayfun(@(c) mean(grp{c}), others));
  29.       si_o(i) = (b_i - a_i) / max(a_i, b_i);
  30.     endfor
  31.     t_orig(end+1) = toc;
  32.   endfor
  33.  
  34.   % BLOCKED: row-by-row computation
  35.   t_blk = [];
  36.   for t = 1:(trials+1)
  37.     tic; si_b = silhouette(X, clust, 'squaredeuclidean', 'DoNotPlot'); t_blk(end+1) = toc;
  38.   endfor
  39.  
  40.   t1 = median(t_orig(2:end)); t2 = median(t_blk(2:end));
  41.   assert(max(abs(si_o - si_b)) < 1e-10, 'Results mismatch!');
  42.  
  43.   ratio = t1/t2; winner = ifelse(ratio > 1, 'Blocked', 'Original');
  44.   printf('%7d %9.4fs %9.4fs %7.2fx %8s\n', n, t1, t2, ratio, winner);
  45.   results = [results; n t1 t2 ratio];
  46. endfor
  47.  
  48. % Save CSV
  49. csvwrite('benchmark_silhouette_results.csv', results);
  50. printf('\nResults saved to benchmark_silhouette_results.csv\n');
  51.  
  52. % Plot
  53. figure('Position', [100 100 900 500]);
  54. loglog(results(:,1), results(:,2), 'b-o', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'b');
  55. hold on;
  56. loglog(results(:,1), results(:,3), 'r-s', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'r');
  57. grid on; set(gca, 'FontSize', 12, 'LineWidth', 1);
  58. xlabel('N (number of points)', 'FontSize', 14, 'FontWeight', 'bold');
  59. ylabel('Time (seconds)', 'FontSize', 14, 'FontWeight', 'bold');
  60. title('Silhouette Performance: Original vs Blocked', 'FontSize', 16, 'FontWeight', 'bold');
  61. legend({'Original (pdist+squareform)', 'Blocked (row-by-row)'}, 'Location', 'northwest', 'FontSize', 12);
  62. set(gca, 'XTick', [10 100 1000 10000], 'XTickLabel', {'10', '100', '1K', '10K'});
  63. print('-dpng', '-r150', 'benchmark_silhouette_plot.png');
  64. printf('Plot saved to benchmark_silhouette_plot.png\n');
  65.  
  66. % Summary
  67. if all(results(:,4) > 1)
  68.   printf('\nāœ“ Blocked is faster for ALL sizes tested - no threshold needed\n');
  69. else
  70.   idx = find(results(:,4) < 1, 1, 'last');
  71.   if ~isempty(idx), printf('\n⚔ Crossover near N=%d\n', results(idx,1)); endif
  72. endif
  73.  
Advertisement
Add Comment
Please, Sign In to add comment