Created
March 27, 2025 22:11
Revisions
-
HParker created this gist
Mar 27, 2025 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ ruby 3.1.6p260 (2024-05-29 revision a777087be6) [arm64-darwin24] Warming up -------------------------------------- to_ary success 1.298M i/100ms Array success 1.844M i/100ms rescue success 1.916M i/100ms Calculating ------------------------------------- to_ary success 12.993M (± 0.5%) i/s (76.96 ns/i) - 66.215M in 5.096195s Array success 18.418M (± 0.2%) i/s (54.30 ns/i) - 92.221M in 5.007224s rescue success 19.166M (± 0.3%) i/s (52.18 ns/i) - 97.726M in 5.098938s Comparison: rescue success: 19166225.0 i/s Array success: 18417669.3 i/s - 1.04x slower to_ary success: 12993380.0 i/s - 1.48x slower ruby 3.1.6p260 (2024-05-29 revision a777087be6) [arm64-darwin24] Warming up -------------------------------------- to_ary fail 218.607k i/100ms Array fail 808.883k i/100ms rescue fail 77.869k i/100ms Calculating ------------------------------------- to_ary fail 2.188M (± 1.0%) i/s (457.10 ns/i) - 11.149M in 5.096679s Array fail 8.114M (± 0.2%) i/s (123.25 ns/i) - 41.253M in 5.084386s rescue fail 770.190k (± 4.8%) i/s (1.30 μs/i) - 3.893M in 5.072462s Comparison: Array fail: 8113689.4 i/s to_ary fail: 2187702.6 i/s - 3.71x slower rescue fail: 770189.7 i/s - 10.53x 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ require 'benchmark/ips' success_obj = [] fail_obj = 1 Benchmark.ips do |x| x.report("to_ary success") do if success_obj.respond_to?(:to_ary) success_obj.to_ary else raise "Wow there" end end x.report("Array success") do Array(success_obj) end x.report("rescue success") do begin success_obj.to_ary rescue NoMethodError raise InvalidCollectionArgumentError end end x.compare! end Benchmark.ips do |x| x.report("to_ary fail") do begin if fail_obj.respond_to?(:to_ary) fail_obj.to_ary else raise "Wow there" end rescue nil end end x.report("Array fail") do Array(fail_obj) end x.report("rescue fail") do begin begin fail_obj.to_ary rescue NoMethodError raise InvalidCollectionArgumentError end rescue nil end end x.compare! end