Beingamanforever

benchmark pdist

Jan 27th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.61 KB | None | 0 0
  1. ## Benchmark script for pdist: 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/pdist_orig.m", "/tmp/pdist_original.m");
  12. copyfile("/tmp/pdist_blocked.m", "/tmp/pdist_new.m");
  13.  
  14. ## Modify function names in copied files
  15. fid = fopen("/tmp/pdist_original.m", "r");
  16. content = char(fread(fid)');
  17. fclose(fid);
  18. content = strrep(content, "function D = pdist (", "function D = pdist_original (");
  19. fid = fopen("/tmp/pdist_original.m", "w");
  20. fwrite(fid, content);
  21. fclose(fid);
  22.  
  23. fid = fopen("/tmp/pdist_new.m", "r");
  24. content = char(fread(fid)');
  25. fclose(fid);
  26. content = strrep(content, "function D = pdist (", "function D = pdist_new (");
  27. fid = fopen("/tmp/pdist_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 (1000), then larger values above
  36. N_values = [5, 10, 20, 30, 50, 75, 100, 200, 250, 300, 400, 500, 600, 700, 800, 900, 1000, 1500, 2000, 2500, 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_pdist_results.csv", "w");
  43. fprintf(fid, "N,t_orig,t_blocked,speedup\n");
  44.  
  45. printf("Benchmarking pdist: original vs blocked (threshold=1000)\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.  
  54.   ## Warmup runs for original
  55.   for w = 1:n_warmup
  56.     D = pdist_original(X);
  57.   endfor
  58.  
  59.   ## Timed runs for original
  60.   times_orig = zeros(1, n_runs);
  61.   for r = 1:n_runs
  62.     tic;
  63.     D = pdist_original(X);
  64.     times_orig(r) = toc;
  65.   endfor
  66.   t_orig = mean(times_orig);
  67.  
  68.   ## Warmup runs for blocked
  69.   for w = 1:n_warmup
  70.     D = pdist_new(X);
  71.   endfor
  72.  
  73.   ## Timed runs for blocked  
  74.   times_blocked = zeros(1, n_runs);
  75.   for r = 1:n_runs
  76.     tic;
  77.     D = pdist_new(X);
  78.     times_blocked(r) = toc;
  79.   endfor
  80.   t_blocked = mean(times_blocked);
  81.  
  82.   speedup = t_orig / t_blocked;
  83.  
  84.   printf("%-6d  %-12.6f  %-12.6f  %-8.4f\n", N, t_orig, t_blocked, speedup);
  85.   fprintf(fid, "%d,%.6f,%.6f,%.4f\n", N, t_orig, t_blocked, speedup);
  86. endfor
  87.  
  88. fclose(fid);
  89. printf("\nResults saved to benchmark_pdist_results.csv\n");
  90.  
  91. ## Cleanup
  92. delete("/tmp/pdist_original.m");
  93. delete("/tmp/pdist_new.m");
  94.  
Advertisement
Add Comment
Please, Sign In to add comment