Created
February 23, 2020 09:23
-
-
Save kenchan/91f1e405ed35d9d9e8305138811f482f to your computer and use it in GitHub Desktop.
Ruby 2.3での階乗計算
This file contains 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
Warming up -------------------------------------- | |
** 70.304k i/100ms | |
my pow 27.298k i/100ms | |
Calculating ------------------------------------- | |
** 1.633M (± 5.7%) i/s - 8.155M in 5.012777s | |
my pow 345.490k (± 3.6%) i/s - 1.747M in 5.063730s | |
Comparison: | |
**: 1632697.1 i/s | |
my pow: 345489.6 i/s - 4.73x slower | |
"2.3.8" | |
true |
This file contains 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' | |
def my_pow(i, n) | |
return 1 if n == 0 | |
x = my_pow(i, n / 2) | |
x = x * x | |
if (n % 2 == 1) | |
x = x * 2 | |
end | |
return x | |
end | |
n = 10 * 9 + 7 | |
Benchmark.ips do |x| | |
x.report("**") do | |
2 ** n | |
end | |
x.report('my pow') do | |
my_pow(2, n) | |
end | |
x.compare! | |
end | |
p RUBY_VERSION | |
p 2 ** n == my_pow(2, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment