From: kryglik Date: 2006-01-02T21:42:58+09:00 Subject: Re: strange speeds Robert Klemme wrote: > kryglik wrote: >> Hello ruby people, >> I've tried to make some speed test in ruby to find out how fast is >> searching in hash (using has_key?) >> >> The results were OK but I discovered curious thing -- when i generate >> big hash the speed is different when i unserialize (marshal.load) the >> same hash. >> >> Unserialized Hash is 1.5x faster in has_key? method. Tried it with >> GC.disable as well, results were same. >> >> What am I missing? >> >> Thanx for any ideas > > The first reason that comes to mind is different insertion order. But I > hardly believe it could make such a big difference. Care to post your > testing code? > > Kind regards > > robert > Well, insertion order will be same, won't be? h = {} h_slovo = [] if not true #HERE YOU SWITCH IF GENERATE/LOAD for x in 1..40_000 if x % 2000 == 0 puts x end slovo = "" for z in 1..32 slovo += (rand(25)+65).chr end if rand(10) == 3 h_slovo << slovo end h.update slovo => rand(1_000_000) slovo = nil end f = File.new("hash.marshal","w+") f.puts(Marshal.dump(h)) f.close f = File.new("hash1.marshal","w+") f.puts(Marshal.dump(h_slovo)) f.close else puts "loading hash" h = Marshal.load(File.open("hash.marshal","r")) puts "loading hash1" h_slovo = Marshal.load(File.open("hash1.marshal","r")) puts "done" end t1 = Time.now for slovo1 in h_slovo h.has_key? h_slovo end t2 = Time.now puts "hash: " + h.size.to_s puts "hled: " + h_slovo.size.to_s puts t2-t1