Skip to content

Instantly share code, notes, and snippets.

@mmozuras
Last active August 29, 2015 14:01
Show Gist options
  • Save mmozuras/33856e4d3f1da91e56d3 to your computer and use it in GitHub Desktop.
Save mmozuras/33856e4d3f1da91e56d3 to your computer and use it in GitHub Desktop.
Benchmark various ways to write ==.*|| in Ruby
require 'benchmark/ips'
require 'set'
ARRAY = [:integer, :float, :decimal]
SET = ARRAY.to_set
type = :decimal
Benchmark.ips do |x|
x.report('array') { [:integer, :float, :decimal].include?(type) }
x.report('or') { type == :integer || type == :float || type == :decimal }
x.report('const array') { ARRAY.include?(type) }
x.report('const set') { SET.include?(type) }
end
@mmozuras
Copy link
Author

ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

           array     82214 i/100ms
              or     87009 i/100ms
     const array     90747 i/100ms
       const set     92533 i/100ms

           array  3517242.6 (±5.3%) i/s -   17593796 in   5.019261s
              or  3897367.8 (±5.8%) i/s -   19490016 in   5.020405s
     const array  4301022.7 (±4.9%) i/s -   21507039 in   5.014122s
       const set  5307965.3 (±7.0%) i/s -   26371905 in   5.002922s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment