Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark'
require 'benchmark/ips'
seed = 1239012
Benchmark.ips do |x|
x.report('bitwise multiplication by 2') { seed << 1 }
x.report('arithmetic multiplication by 2') { seed * 2 }
x.report('bitwise division by 2') { seed >> 1 }
x.report('arithmetic division by 2') { seed / 2 }
x.report('bitwise even') { seed & 1 == 1 }
x.report('arithmetic even') { seed % 2 == 1 }
end
Calculating -------------------------------------
bitwise multiplication by 2 169.075k i/100ms
arithmetic multiplication by 2 174.182k i/100ms
bitwise division by 2 172.457k i/100ms
arithmetic division by 2 176.047k i/100ms
bitwise even 171.047k i/100ms
arithmetic even 174.381k i/100ms
-------------------------------------------------
bitwise multiplication by 2 10.121M (± 1.1%) i/s - 50.722M
arithmetic multiplication by 2 10.888M (± 1.4%) i/s - 54.519M
binary division by 2 10.051M (± 1.9%) i/s - 50.357M
arithmetic division by 2 10.364M (± 3.3%) i/s - 51.758M
binary even 9.601M (± 2.0%) i/s - 48.064M
arithmetic even 10.180M (± 1.7%) i/s - 50.919M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.