From 25e61ad5369f88010429c51ed6c0e38d3c46d4ee Mon Sep 17 00:00:00 2001 From: Krzysztof Rybka Date: Thu, 18 Apr 2019 20:12:57 +0200 Subject: [PATCH] Check objects individually MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit obj1 = 1, obj2 = 1.1, numeric_tolerance = 0.1 Warming up -------------------------------------- original 106.128k i/100ms optimized 161.873k i/100ms Calculating ------------------------------------- original 1.682M (± 5.8%) i/s - 8.384M in 5.005149s optimized 3.560M (± 2.4%) i/s - 17.806M in 5.005019s Comparison: optimized: 3559889.7 i/s original: 1682449.9 i/s - 2.12x slower Calculating ------------------------------------- original 40.000 memsize ( 0.000 retained) 1.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) optimized 0.000 memsize ( 0.000 retained) 0.000 objects ( 0.000 retained) 0.000 strings ( 0.000 retained) Comparison: optimized: 0 allocated original: 40 allocated - Infx more --- lib/hashdiff/util.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/hashdiff/util.rb b/lib/hashdiff/util.rb index 0b10c91..da53cf8 100644 --- a/lib/hashdiff/util.rb +++ b/lib/hashdiff/util.rb @@ -82,8 +82,7 @@ def self.node(hash, parts) # # check for equality or "closeness" within given tolerance def self.compare_values(obj1, obj2, options = {}) - if (options[:numeric_tolerance].is_a? Numeric) && - [obj1, obj2].all? { |v| v.is_a? Numeric } + if options[:numeric_tolerance].is_a?(Numeric) && obj1.is_a?(Numeric) && obj2.is_a?(Numeric) return (obj1 - obj2).abs <= options[:numeric_tolerance] end