Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

Why Ruby `num >> 1` is slower than `num/2`

$
0
0

i surprised that Ruby num >> 1 is slower than num/2 since i always thought that bit operators should faster than normal operators.

# ruby 3.0.3require "benchmark/ips"Benchmark.ips do |bm|    num = 1000000    bm.report("num/2") do        num/2    end    bm.report("num >> 1") do        num >> 1    end    bm.compare!end# num/2:        16045584.8 i/s# num >> 1:     14591335.3 i/s - 1.10x  slowerBenchmark.ips do |bm|    num = 1000000    bm.report("num * 2") do        num * 2    end    bm.report("num << 1") do        num << 1    end    bm.compare!end# num * 2:      18252815.2 i/s# num << 1:     14289700.6 i/s - 1.28x  slower

Viewing all articles
Browse latest Browse all 4615

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>