Created
May 18, 2015 17:56
-
-
Save jgaskins/2b680390cff632ad7b0c to your computer and use it in GitHub Desktop.
Benchmarking Ruby's Math.sqrt vs raising to the power of 1/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Calculating ------------------------------------- | |
sqrt 103.322k i/100ms | |
x ** 0.5 125.661k i/100ms | |
------------------------------------------------- | |
sqrt 2.277M (± 6.1%) i/s - 11.365M | |
x ** 0.5 3.717M (± 5.7%) i/s - 18.598M | |
Comparison: | |
x ** 0.5: 3717095.3 i/s | |
sqrt: 2277003.6 i/s - 1.63x slower |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
Benchmark.ips do |x| | |
x.report('sqrt') { Math.sqrt(rand(1024)) } | |
x.report('x ** 0.5') { rand(1024) ** 0.5 } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ran these on an i7-3517U CPU @ 1.90GHz.
Ruby 2.2:
Ruby 2.3:
Meanwhile Crystal 0.9.1:
;)