-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
require "benchmark" | ||
require "../src/kd_tree" | ||
|
||
# Generate 1 million random points | ||
points = Array.new(1_000_000) { [rand * 100.0, rand * 100.0] } | ||
# Generate 10 millions random points | ||
points = Array.new(10_000_000) { [rand * 100.0, rand * 100.0] } | ||
|
||
puts "Benchmarking KD-Tree with 1 million points" | ||
tree = nil | ||
build_time = Benchmark.measure { | ||
tree = Kd::Tree(Array(Float64)).new(points) | ||
} | ||
puts "Benchmarking KD-Tree with 10 millions points" | ||
|
||
puts "build(init): #{build_time.total.round(2)} seconds" | ||
|
||
tree = tree.not_nil! | ||
Benchmark.bm do |x| | ||
tree = nil | ||
|
||
# Define a test point to find nearest neighbors for | ||
test_point = [50.0, 50.0] | ||
x.report("build(init)") { | ||
tree = Kd::Tree(Array(Float64)).new(points) | ||
} | ||
|
||
Benchmark.bm do |x| | ||
[1, 5, 10, 50, 100, 255, 999].each do |n| | ||
x.report("nearest point #{n.to_s.rjust(3, ' ')}") do | ||
tree.not_nil!.nearest(test_point, n) | ||
1000.times do | ||
test_point = [rand * 100.0, rand * 100.0] | ||
|
||
tree.not_nil!.nearest(test_point, n) | ||
end | ||
end | ||
end | ||
end |