From: "Ara.T.Howard" Date: 2005-06-10T06:25:57+09:00 Subject: Re: python/ruby benchmark. On Fri, 10 Jun 2005, Gyoung-Yoon Noh wrote: > Using 'set' is somewhat faster than Hash in 1.9. > I've rewritten that code using 'set' as following: > > require 'set' > > d = Set.new > > File.open('/usr/share/dict/words') do |f| > d << $_.chop! while f.gets > end > > STDIN.each do |l| > puts l unless d.include? l.chop! > end > > Here are timed results in my machine. hmmm. since set uses hash yet has at least another level of method calls that's hard to believe... this is what i'm getting: harp:~ > ruby a.rb ========< set >======== 3.818156 ========< hash >======== 2.572087 harp:~ > ruby a.rb ========< set >======== 3.824544 ========< hash >======== 2.600603 harp:~ > cat a.rb require 'set' def bench label fork { GC.disable STDOUT.sync = true puts "========< #{ label } >========" a = Time::now.to_f yield b = Time::now.to_f printf "%f\n", b - a puts } Process::wait end words = IO::readlines('/usr/share/dict/words').map{|word| word.strip} set = Set::new hash = Hash::new words.each{|word| set << hash[word] = word} bench('set'){ 42.times{ words.each{|word| set.include? word}}} bench('hash'){ 42.times{ words.each{|word| hash.has_key? word}}} note that times are the time it takes to look up every single word in the list 42 times. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================