Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Float::INFINITYってrangeに使うとどうなんだっけ?
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("2..50") {
case rand(50)
when 1
1
when 2..50
50
else
nil
end
}
x.report("2..Float::INFINITY") {
case rand(50)
when 1
1
when 2..Float::INFINITY
50
else
nil
end
}
x.compare!
end
$ ruby inifity_benchmark.rb
Warming up --------------------------------------
2..50 152.772k i/100ms
2..Float::INFINITY 116.007k i/100ms
Calculating -------------------------------------
2..50 2.604M (± 7.8%) i/s - 12.986M in 5.026051s
2..Float::INFINITY 1.799M (±21.1%) i/s - 8.120M in 5.042590s
Comparison:
2..50: 2603906.8 i/s
2..Float::INFINITY: 1798800.0 i/s - 1.45x slower
$ ruby inifity_benchmark.rb
Warming up --------------------------------------
2..50 171.891k i/100ms
2..Float::INFINITY 135.066k i/100ms
Calculating -------------------------------------
2..50 3.259M (± 8.7%) i/s - 16.330M in 5.058189s
2..Float::INFINITY 2.290M (± 7.5%) i/s - 11.481M in 5.047540s
Comparison:
2..50: 3259256.3 i/s
2..Float::INFINITY: 2289511.3 i/s - 1.42x slower
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.