Skip to content

Instantly share code, notes, and snippets.

@shugo
Created December 5, 2022 06:16
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 shugo/98970fba2f1f325c73cb5543ac2859ba to your computer and use it in GitHub Desktop.
Save shugo/98970fba2f1f325c73cb5543ac2859ba to your computer and use it in GitHub Desktop.
$ cat b2.rb
require 'forwardable'
require 'pp'
require 'benchmark/ips'
class Obj
extend Forwardable
attr_accessor :other
def initialize
@other = Other.new
end
def foo_with_double_splat_with_name(**kw)
@other.foo(**kw)
end
def foo_with_double_splat(**)
@other.foo(**)
end
def foo_with_triple_dots(...)
@other.foo(...)
end
delegate :foo => :@other
end
class Other
def foo(**kw) end
end
o = Obj.new
Benchmark.ips do |x|
x.report 'simple call' do
o.other.foo(x: 1)
end
x.report 'delegate with double splat with name' do
o.foo_with_double_splat_with_name(x: 1)
end
x.report 'delegate with double splat' do
o.foo_with_double_splat(x: 1)
end
x.report 'delegate with triple dots' do
o.foo_with_triple_dots(x: 1)
end
x.report 'delegate via forwardable' do
o.foo(x: 1)
end
end
$ ./ruby-old b2.rb
Warming up --------------------------------------
simple call 628.959k i/100ms
delegate with double splat with name
410.327k i/100ms
delegate with double splat
414.942k i/100ms
delegate with triple dots
280.816k i/100ms
delegate via forwardable
260.609k i/100ms
Calculating -------------------------------------
simple call 6.258M (± 1.0%) i/s - 31.448M in 5.026023s
delegate with double splat with name
4.131M (± 2.2%) i/s - 20.927M in 5.068590s
delegate with double splat
3.938M (±13.8%) i/s - 19.087M in 5.007478s
delegate with triple dots
2.799M (± 1.6%) i/s - 14.041M in 5.017348s
delegate via forwardable
2.573M (± 1.8%) i/s - 13.030M in 5.066372s
excelsior:ruby$ ruby b2.rb
Warming up --------------------------------------
simple call 586.048k i/100ms
delegate with double splat with name
392.506k i/100ms
delegate with double splat
405.514k i/100ms
delegate with triple dots
155.868k i/100ms
delegate via forwardable
256.261k i/100ms
Calculating -------------------------------------
simple call 6.025M (± 2.4%) i/s - 30.474M in 5.061014s
delegate with double splat with name
4.036M (± 1.9%) i/s - 20.410M in 5.058897s
delegate with double splat
4.033M (± 1.3%) i/s - 20.276M in 5.028230s
delegate with triple dots
1.573M (± 2.1%) i/s - 7.949M in 5.056634s
delegate via forwardable
2.554M (± 2.7%) i/s - 12.813M in 5.020564s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment