Skip to content

Instantly share code, notes, and snippets.

@tadman
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadman/8741869 to your computer and use it in GitHub Desktop.
Save tadman/8741869 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'benchmark'
count = 10e6.to_i
Benchmark.bm do |x|
x.report(:mod) { count.times { |n| n % 2 == 0 } }
x.report(:and) { count.times { |n| n & 1 == 0 } }
x.report(:odd) { count.times { |n| n.even? } }
end
# user system total real
# mod 0.780000 0.000000 0.780000 ( 0.775271)
# and 0.740000 0.000000 0.740000 ( 0.735793)
# odd 0.650000 0.000000 0.650000 ( 0.659626)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment