I wrote a simple test:
require 'benchmark' l = 0 funcs = [ proc { l == 0 }, proc { l.zero? }, ] def ctime func time = 0 1000.times { time += Benchmark.measure { 1000.times { func.call } }.to_a[5].to_f } rtime = time /= 1000000 end funcs.each {| func | p ctime( func ) }
As you can see, the method :zero? It takes several additional (about 10%) methods against the method :== , so it is slower than :== . Secondly, since the method :zero? only included in the Numeric class and its descendants, you can use it only for numbers.
source share