Skip to content

Commit

Permalink
Update benchmark.cr
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 12, 2024
1 parent d2fe835 commit 931657c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions benchmark/benchmark.cr
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

0 comments on commit 931657c

Please sign in to comment.