File: transfer_nested_constants.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 (77 lines) | stat: -rw-r--r-- 2,284 bytes parent folder | download | duplicates (6)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))

require 'benchmark'
require 'rspec/mocks'

N = ENV.fetch('N', 10_000).to_i
M = ENV.fetch('M', 5).to_i

puts "#{N} times, #{M} constants - ruby #{RUBY_VERSION}"
puts

class A
  M.times do |x|
    const_set("C#{x}", Object.new)
  end
end

Benchmark.bm(20) do |bm|
  RSpec::Mocks.setup(self)

  bm.report("with constants") do
    N.times do
      class_double('A').as_stubbed_const(:transfer_nested_constants => true)
    end
  end

  bm.report("without constants") do
    N.times do
      class_double('A').as_stubbed_const(:transfer_nested_constants => false)
    end
  end
end

# > for n in 1 10000; do for m in 0 5 100; do echo; \
#     env N=$n M=$m ruby benchmarks/transfer_nested_constants.rb; \
#   echo; done; done
#
# 1 times, 0 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         0.000000   0.000000   0.000000 (  0.000180)
# without constants      0.000000   0.000000   0.000000 (  0.000071)
#
#
# 1 times, 5 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         0.000000   0.000000   0.000000 (  0.000197)
# without constants      0.000000   0.000000   0.000000 (  0.000123)
#
#
# 1 times, 100 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         0.000000   0.000000   0.000000 (  0.000433)
# without constants      0.000000   0.000000   0.000000 (  0.000115)
#
#
# 10000 times, 0 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         0.900000   0.020000   0.920000 (  0.935583)
# without constants      0.660000   0.010000   0.670000 (  0.680178)
#
#
# 10000 times, 5 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         1.080000   0.020000   1.100000 (  1.114722)
# without constants      0.720000   0.020000   0.740000 (  0.741976)
#
#
# 10000 times, 100 constants - ruby 2.0.0
#
#                            user     system      total        real
# with constants         3.870000   0.110000   3.980000 (  4.000176)
# without constants      0.930000   0.010000   0.940000 (  0.947197)