From: "Jonas Pfenniger (zimbatm)" Date: 2011-01-13T21:39:01+09:00 Subject: Re: Why {} is much faster than Hash.new ? I think it's the cost of the method call with options parsing. Benchmark.realtime do 1_000_000.times do @m = Hash.new end end => 1.1976041793823242 def blank_method; end Benchmark.realtime do 1_000_000.times do @m = blank_method end end => 0.1965808868408203 Benchmark.realtime do 1_000_000.times do @m = {} end end => 0.2893531322479248 Benchmark.realtime do 1_000_000.times do @m = Hash.allocate end end => 0.3925323486328125 You see that Hash.allocate's time is about the same as ({} + blank_method). The rest comes from option parsing I guess. 2011/1/13 Iñaki Baz Castillo : > Hi, a simple benchmark: > > Benchmark.realtime { 1000000.times { @m = Hash.new } } > => 0.6552023887634277 > > Benchmark.realtime { 1000000.times { @m = {} } } > => 0.17668819427490234 > > I would like to know why {} is so much faster. I can understand that > Hash.initialize method must inspect is an argument is given and so, is > just due to it? > > Thanks. > > > -- > Iñaki Baz Castillo > > >