File: accessing_configuration_via_method_vs_cache.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 (52 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (4)
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
44
45
46
47
48
49
50
51
52
require 'benchmark'

n = 10_000

require 'rspec/mocks'

# precache config
RSpec::Mocks.configuration

Benchmark.benchmark do |bm|
  puts "#{n} times - ruby #{RUBY_VERSION}"

  puts
  puts "directly"

  3.times do
    bm.report do
      n.times do
        original_state = RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification
        RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification = true
        RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification = original_state
      end
    end
  end

  puts
  puts "with cached value"

  3.times do
    bm.report do
      n.times do
        config = RSpec::Mocks.configuration
        original_state = config.temporarily_suppress_partial_double_verification
        config.temporarily_suppress_partial_double_verification = true
        config.temporarily_suppress_partial_double_verification = original_state
      end
    end
  end
end

__END__
10000 times - ruby 2.3.1

directly
   0.000000   0.000000   0.000000 (  0.002654)
   0.000000   0.000000   0.000000 (  0.002647)
   0.010000   0.000000   0.010000 (  0.002645)

with cached value
   0.000000   0.000000   0.000000 (  0.001386)
   0.000000   0.000000   0.000000 (  0.001387)
   0.000000   0.000000   0.000000 (  0.001399)