Skip to content

Instantly share code, notes, and snippets.

@chancancode
Created October 22, 2014 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancancode/02738ef24afec3a2eb5d to your computer and use it in GitHub Desktop.
Save chancancode/02738ef24afec3a2eb5d to your computer and use it in GitHub Desktop.
class Object
def try(*a, &b)
if a.empty? && block_given?
if b.arity.zero?
instance_eval(&b)
else
yield self
end
else
public_send(*a, &b) if respond_to?(a.first)
end
end
def try_delegated!(*a, &b)
if a.empty? && block_given?
try(*a, &b)
else
public_send(*a, &b)
end
end
def try_duplicated!(*a, &b)
if a.empty? && block_given?
if b.arity.zero?
instance_eval(&b)
else
yield self
end
else
public_send(*a, &b)
end
end
end
class NilClass
def try(*args)
nil
end
def try_delegated!(*args)
nil
end
def try_duplicated!(*args)
nil
end
end
require 'benchmark/ips'
NIL = nil
NOT_NIL = ''
Benchmark.ips do |x|
x.report('delegated nil without block') { NIL.try_delegated!(:length) }
x.report('duplicated nil without block') { NIL.try_duplicated!(:length) }
x.report('delegated nil with instance_evaled block') { NIL.try_delegated! { length } }
x.report('duplicated nil with instance_evaled block') { NIL.try_duplicated! { length } }
x.report('delegated nil with yielded block') { NIL.try_delegated! { |s| s.length } }
x.report('duplicated nil with yielded block') { NIL.try_duplicated! { |s| s.length } }
x.report('delegated not nil without block') { NOT_NIL.try_delegated!(:length) }
x.report('duplicated not nil without block') { NOT_NIL.try_duplicated!(:length) }
x.report('delegated not nil with instance_evaled block') { NOT_NIL.try_delegated! { length } }
x.report('duplicated not nil with instance_evaled block') { NOT_NIL.try_duplicated! { length } }
x.report('delegated not nil with yielded block') { NOT_NIL.try_delegated! { |s| s.length } }
x.report('duplicated not nil with yielded block') { NOT_NIL.try_duplicated! { |s| s.length } }
end
@chancancode
Copy link
Author

Results:

delegated nil without block
                      4810645.8 (±5.6%) i/s -   23980340 in   5.000539s
duplicated nil without block
                      4788780.7 (±5.1%) i/s -   23863320 in   4.997106s
delegated nil with instance_evaled block
                      5376855.2 (±5.3%) i/s -   26809566 in   4.999588s
duplicated nil with instance_evaled block
                      5388146.9 (±5.4%) i/s -   26909952 in   5.008800s
delegated nil with yielded block
                      5346797.7 (±5.9%) i/s -   26710488 in   5.013989s
duplicated nil with yielded block
                      5312825.4 (±5.7%) i/s -   26472428 in   4.999669s
delegated not nil without block
                      3061581.9 (±5.6%) i/s -   15301916 in   5.017537s
duplicated not nil without block
                      3063588.7 (±3.9%) i/s -   15301110 in   5.002198s
delegated not nil with instance_evaled block
                       702804.5 (±12.1%) i/s -    3478140 in   5.013572s
duplicated not nil with instance_evaled block
                       782611.5 (±12.3%) i/s -    3865589 in   5.001701s
delegated not nil with yielded block
                       777353.9 (±13.4%) i/s -    3867036 in   5.048989s
duplicated not nil with yielded block
                       904131.6 (±11.4%) i/s -    4484181 in   5.003097s

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