File: class_exec_vs_klass_exec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (43 lines) | stat: -rw-r--r-- 1,212 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'benchmark/ips'
require 'rspec/support'
require 'rspec/support/with_keywords_when_needed'

Klass = Class.new do
  def test(*args, **kwargs)
  end
end

def class_exec_args
  Klass.class_exec(:a, :b) { }
end

def klass_exec_args
  RSpec::Support::WithKeywordsWhenNeeded.class_exec(Klass, :a, :b) { }
end

def class_exec_kw_args
  Klass.class_exec(a: :b) { |a:| }
end

def klass_exec_kw_args
  RSpec::Support::WithKeywordsWhenNeeded.class_exec(Klass, a: :b) { |a:| }
end

Benchmark.ips do |x|
  x.report("class_exec(*args)          ") { class_exec_args }
  x.report("klass_exec(*args)          ") { klass_exec_args }
  x.report("class_exec(*args, **kwargs)") { class_exec_kw_args }
  x.report("klass_exec(*args, **kwargs)") { klass_exec_kw_args }
end

__END__

Calculating -------------------------------------
class_exec(*args)
                          5.555M (± 1.6%) i/s -     27.864M in   5.017682s
klass_exec(*args)
                        657.945k (± 4.6%) i/s -      3.315M in   5.051511s
class_exec(*args, **kwargs)
                          2.882M (± 3.3%) i/s -     14.555M in   5.056905s
klass_exec(*args, **kwargs)
                         52.710k (± 4.1%) i/s -    265.188k in   5.041218s