Skip to content

Instantly share code, notes, and snippets.

@akshaymohite
Last active November 14, 2017 07:41
Show Gist options
  • Save akshaymohite/645bc240888f32469f45c31fcc166740 to your computer and use it in GitHub Desktop.
Save akshaymohite/645bc240888f32469f45c31fcc166740 to your computer and use it in GitHub Desktop.
comparison.rb
params = {a: 4, b: 5, b: 4, to: 'akshay'}
mandatory_keys = [:from, :to, :caller_id, :call_type]
keys = params.keys
Benchmark.ips do |benchmark|
benchmark.report('new') { mandatory_keys.any?{|key| keys.include?(key)} }
benchmark.report('old') { mandatory_keys.all?{|key| params.keys.include?(key)} }
benchmark.compare!
end
Warming up --------------------------------------
new 148.784k i/100ms
old 117.088k i/100ms
Calculating -------------------------------------
new 2.727M (± 7.5%) i/s - 13.688M in 5.048651s
old 1.888M (± 4.9%) i/s - 9.484M in 5.037043s
Comparison:
new: 2727112.1 i/s
old: 1887710.7 i/s - 1.44x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment