-
-
Save eregon/8e104d291ca64d0834b580d18b837059 to your computer and use it in GitHub Desktop.
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 fib_while(n) | |
a = 0 | |
b = 1 | |
while n > 0 | |
a, b = b, a + b | |
n -= 1 | |
end | |
a | |
end | |
def fib_times(n) | |
a = 0 | |
b = 1 | |
n.times { a, b = b, a + b } | |
a | |
end | |
def fib_times_nil(n) | |
a = 0 | |
b = 1 | |
n.times { a, b = b, a + b; nil } | |
a | |
end | |
Benchmark.ips do |x| | |
x.report 'fib_while(30)' do | |
fib_while(30) | |
end | |
x.report 'fib_times(30)' do | |
fib_times(30) | |
end | |
x.report 'fib_times_nil(30)' do | |
fib_times_nil(30) | |
end | |
end |
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
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-linux] | |
Warming up -------------------------------------- | |
fib_while(30) 784.380k i/100ms | |
fib_times(30) 44.572k i/100ms | |
fib_times_nil(30) 100.193k i/100ms | |
Calculating ------------------------------------- | |
fib_while(30) 8.360M (± 0.2%) i/s (119.62 ns/i) - 42.357M in 5.066855s | |
fib_times(30) 444.412k (± 0.1%) i/s (2.25 μs/i) - 2.229M in 5.014717s | |
fib_times_nil(30) 1.018M (± 0.2%) i/s (982.32 ns/i) - 5.110M in 5.019506s | |
truffleruby 24.0.1, like ruby 3.2.2, Oracle GraalVM Native [x86_64-linux] | |
Warming up -------------------------------------- | |
fib_while(30) 37.796M i/100ms | |
fib_times(30) 38.008M i/100ms | |
fib_times_nil(30) 38.025M i/100ms | |
Calculating ------------------------------------- | |
fib_while(30) 379.649M (± 1.0%) i/s (2.63 ns/i) - 1.928B in 5.077866s | |
fib_times(30) 379.985M (± 0.3%) i/s (2.63 ns/i) - 1.900B in 5.001354s | |
fib_times_nil(30) 379.773M (± 0.4%) i/s (2.63 ns/i) - 1.901B in 5.006369s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment