Sha256: 8ce0a15e8b2762b3eaeb074ac79258dff0c3687e1c8b67b05adc400de0c4004a
Contents?: true
Size: 399 Bytes
Versions: 20
Compression:
Stored size: 399 Bytes
Contents
require 'benchmark' class Integer def fact1 return 1 if zero? f = 1 2.upto(self) { |n| f *= n } f end def fact2 (2..self).inject(1){|fact,i| fact*i} end end Benchmark.bm do |b| n = 1000 b.report("current") do n.times do (0..20).each {|i| i.fact1} end end b.report("inject") do n.times do (0..20).each {|i| i.fact2} end end end
Version data entries
20 entries across 20 versions & 1 rubygems