Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Benchmark silhouette: Blocked vs Original implementation
- addpath('inst');
- dims = 10; k = 5; trials = 3;
- sizes = [5 10 50 100 200 350 500 600 750 900 1000 1250 1500 2000 3000 4000 5000 6000 7000 8000 10000];
- printf('\n%7s %10s %10s %8s %8s\n', 'N', 'Original', 'Blocked', 'Ratio', 'Winner');
- printf('%s\n', repmat('-', 1, 50));
- results = [];
- for n = sizes
- if (n*n*8 > 8e9), printf('%7d SKIPPED (>8GB)\n', n); continue; endif
- rand('seed', 12345); X = rand(n, dims); clust = randi(k, n, 1);
- clusterIDs = unique(clust); m = length(clusterIDs);
- % ORIGINAL: pdist + squareform + full matrix loop
- t_orig = [];
- for t = 1:(trials+1)
- tic;
- distMatrix = squareform(pdist(X, 'squaredeuclidean'));
- si_o = zeros(n, 1);
- for i = 1:n
- grp = {}; for j = 1:m, grp{clusterIDs(j)} = distMatrix(i, clust == clusterIDs(j)); endfor
- if length(grp{clust(i)}) == 1, si_o(i) = 1; continue; endif
- a_i = sum(grp{clust(i)}) / (length(grp{clust(i)}) - 1);
- others = clusterIDs(clusterIDs != clust(i));
- b_i = min(arrayfun(@(c) mean(grp{c}), others));
- si_o(i) = (b_i - a_i) / max(a_i, b_i);
- endfor
- t_orig(end+1) = toc;
- endfor
- % BLOCKED: row-by-row computation
- t_blk = [];
- for t = 1:(trials+1)
- tic; si_b = silhouette(X, clust, 'squaredeuclidean', 'DoNotPlot'); t_blk(end+1) = toc;
- endfor
- t1 = median(t_orig(2:end)); t2 = median(t_blk(2:end));
- assert(max(abs(si_o - si_b)) < 1e-10, 'Results mismatch!');
- ratio = t1/t2; winner = ifelse(ratio > 1, 'Blocked', 'Original');
- printf('%7d %9.4fs %9.4fs %7.2fx %8s\n', n, t1, t2, ratio, winner);
- results = [results; n t1 t2 ratio];
- endfor
- % Save CSV
- csvwrite('benchmark_silhouette_results.csv', results);
- printf('\nResults saved to benchmark_silhouette_results.csv\n');
- % Plot
- figure('Position', [100 100 900 500]);
- loglog(results(:,1), results(:,2), 'b-o', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'b');
- hold on;
- loglog(results(:,1), results(:,3), 'r-s', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'r');
- grid on; set(gca, 'FontSize', 12, 'LineWidth', 1);
- xlabel('N (number of points)', 'FontSize', 14, 'FontWeight', 'bold');
- ylabel('Time (seconds)', 'FontSize', 14, 'FontWeight', 'bold');
- title('Silhouette Performance: Original vs Blocked', 'FontSize', 16, 'FontWeight', 'bold');
- legend({'Original (pdist+squareform)', 'Blocked (row-by-row)'}, 'Location', 'northwest', 'FontSize', 12);
- set(gca, 'XTick', [10 100 1000 10000], 'XTickLabel', {'10', '100', '1K', '10K'});
- print('-dpng', '-r150', 'benchmark_silhouette_plot.png');
- printf('Plot saved to benchmark_silhouette_plot.png\n');
- % Summary
- if all(results(:,4) > 1)
- printf('\nā Blocked is faster for ALL sizes tested - no threshold needed\n');
- else
- idx = find(results(:,4) < 1, 1, 'last');
- if ~isempty(idx), printf('\nā” Crossover near N=%d\n', results(idx,1)); endif
- endif
Advertisement
Add Comment
Please, Sign In to add comment