lib/fuzzy_realty.rb in rkneufeld-fuzzy-realty-0.5.1 vs lib/fuzzy_realty.rb in rkneufeld-fuzzy-realty-0.6.2

- old
+ new

@@ -1,5 +1,7 @@ +require 'benchmark' + require 'weights.rb' require 'classes.rb' require 'scores_table.rb' require 'rulebase.rb' @@ -24,38 +26,54 @@ end scores << { :score => score, :listing => listing} end return scores.sort {|a,b| b[:score] <=> a[:score]} end + def self.performance + puts "Benchmarking search through 100,000-1 (powers of ten) random listings" + puts "=======================================================================" + listings = [] + 100_000.times { listings << Listing.random } + puts "Generated #{listings.count} random listings" + Benchmark.bm do |x| + x.report("100,000 listings:") { ExpertSystem.scores(listings,Query.random) } + listings = listings[(0...10_000)] + x.report("10,000 listings:") { ExpertSystem.scores(listings,Query.random) } + listings = listings[(0...1_000)] + x.report("1,000 listings:") { ExpertSystem.scores(listings,Query.random) } + listings = listings[(0...100)] + x.report("100 listings:") { ExpertSystem.scores(listings,Query.random) } + listings = listings[(0...10)] + x.report("10 listings:") { ExpertSystem.scores(listings,Query.random) } + listings = listings[(0...1)] + x.report("1 listing:") { ExpertSystem.scores(listings,Query.random) } + end + end end end #When running the library directly calculate an example if __FILE__ == $0 listings = [] - 100.times do |i| - listings << FuzzyRealty::Listing.new({ - :price => 20_000 + rand(250_000), - :sqft => 300 + rand(2000), - :location => %W{A B C D}[rand(4)], - :style => %W{Bungalow Bi-level Split-level Two-story Condominium}[rand(5)] - }) + 1000.times do |i| + listings << FuzzyRealty::Listing.random end query = FuzzyRealty::Query.new query << FuzzyRealty::Parameter.new(:price,250000) query << FuzzyRealty::Parameter.new(:location, 'A',true) query << FuzzyRealty::Parameter.new(:style,"Condominium",true) query << FuzzyRealty::Parameter.new(:sqft,1575,true) scores = FuzzyRealty::ExpertSystem.scores(listings,query) puts "Query 1, $250k Condominium in the prestiguous 'A' suburbs. 1575 sq. ft." - puts "Top 20 Listings:" - scores[(0..20)].each do |score| - puts "%.2f" % score[:score] + "\t\t#{score[:listing].inspect}" + puts "Top 10 Listings:" + scores[(0...10)].each_with_index do |score,i| + puts "(#{i+1})\t%.2f" % score[:score] + "\t#{score[:listing].inspect}" end puts "\n" + query = FuzzyRealty::Query.new query << FuzzyRealty::Parameter.new(:price,99_000,true) query << FuzzyRealty::Parameter.new(:location,'C') query << FuzzyRealty::Parameter.new(:style, "Bungalow") query << FuzzyRealty::Parameter.new(:sqft,1200) \ No newline at end of file