Skip to content

Instantly share code, notes, and snippets.

@ccocchi
Last active August 29, 2015 14:07
Show Gist options
  • Save ccocchi/b74045f5e3c15db44dce to your computer and use it in GitHub Desktop.
Save ccocchi/b74045f5e3c15db44dce to your computer and use it in GitHub Desktop.
Are you my father?
require 'benchmark/ips'
require 'active_support/inflector'
require 'set'
class A; end
class B < A; end
klasses = [Struct, A].freeze
strings = ['Struct', 'A'].freeze
set = Set.new(strings)
Benchmark.ips do |x|
x.report('klass') { klasses.none? { |k| B <= k } }
x.report('const') { strings.none? { |k| B <= k.constantize } }
x.report('strings') { (strings & B.ancestors.map(&:name)).empty? }
x.report('set') { B.ancestors.none? { |a| set.include? a.name } }
end
# Calculating -------------------------------------
# klass 76805 i/100ms
# const 23443 i/100ms
# strings 23379 i/100ms
# set 41934 i/100ms
# -------------------------------------------------
# klass 1906953.4 (±3.5%) i/s - 9523820 in 5.000878s
# const 362407.1 (±3.3%) i/s - 1828554 in 5.051450s
# strings 331800.5 (±3.6%) i/s - 1659909 in 5.009901s
# set 733412.7 (±3.4%) i/s - 3690192 in 5.037502s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment