Beingamanforever

benchmark pdist2

Jan 27th, 2026
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.70 KB | None | 0 0
  1. ## Benchmark script for pdist2: original vs blocked
  2. ## Outputs CSV with N, t_orig, t_blocked, speedup
  3.  
  4. clear all;
  5.  
  6. ## Add paths for implementations
  7. addpath("/tmp");
  8. addpath("inst");
  9.  
  10. ## Create renamed copies to avoid function name conflicts
  11. copyfile("/tmp/pdist2_orig.m", "/tmp/pdist2_original.m");
  12. copyfile("/tmp/pdist2_blocked.m", "/tmp/pdist2_new.m");
  13.  
  14. ## Modify function names in copied files
  15. fid = fopen("/tmp/pdist2_original.m", "r");
  16. content = char(fread(fid)');
  17. fclose(fid);
  18. content = strrep(content, "function [D, I] = pdist2 (", "function [D, I] = pdist2_original (");
  19. fid = fopen("/tmp/pdist2_original.m", "w");
  20. fwrite(fid, content);
  21. fclose(fid);
  22.  
  23. fid = fopen("/tmp/pdist2_new.m", "r");
  24. content = char(fread(fid)');
  25. fclose(fid);
  26. content = strrep(content, "function [D, I] = pdist2 (", "function [D, I] = pdist2_new (");
  27. fid = fopen("/tmp/pdist2_new.m", "w");
  28. fwrite(fid, content);
  29. fclose(fid);
  30.  
  31. ## Clear function cache and reload
  32. clear functions;
  33. rehash;
  34.  
  35. ## Test N values: small values below threshold (500), then larger values above
  36. N_values = [5, 10, 20, 30, 50, 75, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800, 1000, 1500, 2000, 3000, 4000, 5000];
  37. P = 50;  # dimensions
  38. n_runs = 5;  # number of timed runs
  39. n_warmup = 3;  # warmup runs
  40.  
  41. ## Open CSV file
  42. fid = fopen("benchmark_pdist2_results.csv", "w");
  43. fprintf(fid, "N,t_orig,t_blocked,speedup\n");
  44.  
  45. printf("Benchmarking pdist2: original vs blocked (threshold=500)\n");
  46. printf("=========================================================\n");
  47. printf("%-6s  %-12s  %-12s  %-8s\n", "N", "t_orig", "t_blocked", "speedup");
  48. printf("------  ------------  ------------  --------\n");
  49.  
  50. for n_idx = 1:length(N_values)
  51.   N = N_values(n_idx);
  52.   X = rand(N, P);
  53.   Y = rand(N, P);  # square case M=N
  54.  
  55.   ## Warmup runs for original
  56.   for w = 1:n_warmup
  57.     D = pdist2_original(X, Y);
  58.   endfor
  59.  
  60.   ## Timed runs for original
  61.   times_orig = zeros(1, n_runs);
  62.   for r = 1:n_runs
  63.     tic;
  64.     D = pdist2_original(X, Y);
  65.     times_orig(r) = toc;
  66.   endfor
  67.   t_orig = mean(times_orig);
  68.  
  69.   ## Warmup runs for blocked
  70.   for w = 1:n_warmup
  71.     D = pdist2_new(X, Y);
  72.   endfor
  73.  
  74.   ## Timed runs for blocked  
  75.   times_blocked = zeros(1, n_runs);
  76.   for r = 1:n_runs
  77.     tic;
  78.     D = pdist2_new(X, Y);
  79.     times_blocked(r) = toc;
  80.   endfor
  81.   t_blocked = mean(times_blocked);
  82.  
  83.   speedup = t_orig / t_blocked;
  84.  
  85.   printf("%-6d  %-12.6f  %-12.6f  %-8.4f\n", N, t_orig, t_blocked, speedup);
  86.   fprintf(fid, "%d,%.6f,%.6f,%.4f\n", N, t_orig, t_blocked, speedup);
  87. endfor
  88.  
  89. fclose(fid);
  90. printf("\nResults saved to benchmark_pdist2_results.csv\n");
  91.  
  92. ## Cleanup
  93. delete("/tmp/pdist2_original.m");
  94. delete("/tmp/pdist2_new.m");
  95.  
Advertisement
Add Comment
Please, Sign In to add comment