Skip to content

Instantly share code, notes, and snippets.

@charliedavi
Created April 6, 2012 11:20
Show Gist options
  • Select an option

  • Save charliedavi/2318959 to your computer and use it in GitHub Desktop.

Select an option

Save charliedavi/2318959 to your computer and use it in GitHub Desktop.
charlie@Charlie-Vostro ~/tech/dev/lab/kdtree $ gem install tupalo-kdtree
Building native extensions. This could take a while...
Successfully installed tupalo-kdtree-0.2.3
1 gem installed
charlie@Charlie-Vostro ~/tech/dev/lab/kdtree $ ruby newtest.rb
LoadError: no such file to load -- kdtree
require at org/jruby/RubyKernel.java:1033
require at /home/charlie/.rvm/rubies/jruby-1.6.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
(root) at newtest.rb:2
charlie@Charlie-Vostro ~/tech/dev/lab/kdtree $
code:
require "rubygems"
require "kdtree"
require "benchmark"
def setup_tree(len)
@points = (0...len).map { |i| [rand_coord, rand_coord, i ] }
@kdtree = KDTree.new(@points)
end
def distance(a, b)
x, y = a[0] - b[0], a[1] - b[1]
x * x + y * y
end
def rand_coord
rand(0) * 10 - 5
end
def test_nearestk
pt = []
@list = []
2000000.times do
pt = [rand_coord, rand_coord]
# kdtree search
@list << @kdtree.nearest(pt[0],pt[1]) #puts pt
end
puts "Points from search #{@list.size}"
puts "Points in graph #{@kdsize}"
end
@kdsize = 2000
setup_tree(@kdsize)
Benchmark.bm do |x|
x.report do
test_nearestk
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment