From: Ammar Ali Date: 2010-10-07T13:57:31+09:00 Subject: Re: Benchmarks Project and example for `||=` --001517447efa279fd20491ffbb1c Content-Type: text/plain; charset=UTF-8 On Thu, Oct 7, 2010 at 7:28 AM, Intransition wrote: > Has anyone ever put together a suite of benchmarks that simply measure > interesting Ruby practices? For example, here is a comparison of using > ||= vs not using it. > > n = 1000000 > > puts "#{n} Times" > > t = Time.now > > @b = :b > n.times do > @b > end > > puts "Simple Read : #{Time.now - t} seconds" > > t = Time.now > > n.times do > @a ||= :a > end > > puts " Or Equals : #{Time.now - t} seconds" > > On `ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]`: > > 1000000 Times > Simple Read : 0.395487 seconds > Or Equals : 0.508428 seconds > > On `ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]`: > > 1000000 Times > Simple Read : 0.121280356 seconds > Or Equals : 0.183629043 seconds > > Might be an interesting project. > > It would be interesting to have performance comparisons of the various idioms in common use. In these examples, however, it's not surprising that the first loop (without ||=) would be faster. It's not doing anything really. The second involves testing wether the variable is defined or not, every iteration. Regards, Ammar --001517447efa279fd20491ffbb1c--