Beingamanforever

benchmark kdtreesearcher

Feb 1st, 2026
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.88 KB | None | 0 0
  1. ## Comprehensive KDTree Performance Benchmark
  2. ## Tests various combinations of n (data points), d (dimensions)
  3. ## Outputs CSV results for analysis
  4.  
  5. clear all;
  6.  
  7. ## Test configurations
  8. n_values = [1000, 2500, 5000, 10000, 20000];
  9. d_values = [2, 3, 5, 10, 20];
  10. n_queries = 200;
  11. k_neighbors = 5;
  12. n_trials = 3;  ## Number of trials per configuration
  13.  
  14. ## Initialize results storage
  15. results = {};
  16. row_idx = 1;
  17.  
  18. printf("Running comprehensive KDTree benchmark...\n");
  19. printf("Configurations: %d\n", length(n_values) * length(d_values));
  20. printf("Trials per config: %d\n\n", n_trials);
  21.  
  22. for n = n_values
  23.   for d = d_values
  24.     printf("Testing n=%d, d=%d... ", n, d);
  25.    
  26.     ## Generate random data
  27.     rng(42);  ## Fixed seed for reproducibility
  28.     X_train = rand(n, d);
  29.     X_query = rand(n_queries, d);
  30.    
  31.     ## Warmup (JIT compilation)
  32.     kdtree = KDTreeSearcher(X_train(1:100, :));
  33.     [idx, dist] = knnsearch(kdtree, X_query(1:10, :), 'K', k_neighbors);
  34.    
  35.     ## Time build phase
  36.     build_times = zeros(1, n_trials);
  37.     for trial = 1:n_trials
  38.       tic;
  39.       kdtree = KDTreeSearcher(X_train);
  40.       build_times(trial) = toc;
  41.     endfor
  42.     avg_build = mean(build_times);
  43.    
  44.     ## Time query phase
  45.     query_times = zeros(1, n_trials);
  46.     for trial = 1:n_trials
  47.       kdtree = KDTreeSearcher(X_train);
  48.       tic;
  49.       [idx, dist] = knnsearch(kdtree, X_query, 'K', k_neighbors);
  50.       query_times(trial) = toc;
  51.     endfor
  52.     avg_query = mean(query_times);
  53.    
  54.     ## Store results
  55.     results{row_idx, 1} = n;
  56.     results{row_idx, 2} = d;
  57.     results{row_idx, 3} = avg_build;
  58.     results{row_idx, 4} = avg_query;
  59.     results{row_idx, 5} = avg_build + avg_query;
  60.     results{row_idx, 6} = avg_query / n_queries;  ## per-query time
  61.    
  62.     printf("Build: %.4fs, Query: %.4fs, Total: %.4fs\n", avg_build, avg_query, avg_build + avg_query);
  63.    
  64.     row_idx = row_idx + 1;
  65.   endfor
  66. endfor
  67.  
  68. ## Convert to matrix for easier handling
  69. n_configs = length(n_values) * length(d_values);
  70. result_matrix = zeros(n_configs, 6);
  71. for i = 1:n_configs
  72.   for j = 1:6
  73.     result_matrix(i, j) = results{i, j};
  74.   endfor
  75. endfor
  76.  
  77. ## Save as CSV
  78. csv_filename = "kdtree_benchmark_results.csv";
  79. fid = fopen(csv_filename, "w");
  80. fprintf(fid, "n,d,build_time,query_time,total_time,per_query_time\n");
  81. for i = 1:n_configs
  82.   fprintf(fid, "%d,%d,%.6f,%.6f,%.6f,%.6f\n", ...
  83.           result_matrix(i,1), result_matrix(i,2), result_matrix(i,3), ...
  84.           result_matrix(i,4), result_matrix(i,5), result_matrix(i,6));
  85. endfor
  86. fclose(fid);
  87.  
  88. printf("\n=== Summary Statistics ===\n");
  89. printf("Total configurations tested: %d\n", n_configs);
  90. printf("Build time range: %.4f - %.4f s\n", min(result_matrix(:,3)), max(result_matrix(:,3)));
  91. printf("Query time range: %.4f - %.4f s\n", min(result_matrix(:,4)), max(result_matrix(:,4)));
  92. printf("Results saved to: %s\n", csv_filename);
  93.  
Advertisement
Add Comment
Please, Sign In to add comment