Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "benchmark/ips"
- FIRST_CASE = ["house", "home", "building"]
- SECOND_CASE = ["boat", "vessel", "ship"]
- ARGUMENT = "vessel"
- def if_include_test
- if FIRST_CASE.include?(ARGUMENT)
- :first
- elsif SECOND_CASE.include?(ARGUMENT)
- :second
- else
- nil
- end
- end
- def case_expand_test
- case ARGUMENT
- when *FIRST_CASE then :first
- when *SECOND_CASE then :second
- else
- nil
- end
- end
- Benchmark.ips do |x|
- x.report("if + include?") { if_include_test }
- x.report("case + expand") { case_expand_test }
- x.compare!
- end
- =begin
- Warming up --------------------------------------
- if + include? 544.249k i/100ms
- case + expand 335.889k i/100ms
- Calculating -------------------------------------
- if + include? 5.255M (± 9.5%) i/s - 26.124M in 5.022296s
- case + expand 3.389M (± 8.3%) i/s - 17.130M in 5.089028s
- Comparison:
- if + include?: 5254775.2 i/s
- case + expand: 3388705.3 i/s - 1.55x (± 0.00) slower
- =end
Advertisement
Add Comment
Please, Sign In to add comment