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.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]

           array     85044 i/100ms
              or     91238 i/100ms
     const array     92035 i/100ms
       const set     93557 i/100ms

           array  3222411.3 (±4.9%) i/s -   16073316 in   5.001778s
              or  3948995.4 (±5.9%) i/s -   19707408 in   5.011728s
     const array  3971430.7 (±5.1%) i/s -   19879560 in   5.020253s
       const set  4847490.3 (±5.2%) i/s -   24231263 in   5.014226s

@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