File: method_defined_at_any_visibility.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 (101 lines) | stat: -rw-r--r-- 2,717 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'benchmark'

n = 1_000_000

Foo = Class.new do
  1.upto(n) do |i|
    define_method(:"public_method_#{i}") {}
    define_method(:"protected_method_#{i}") {}
    protected :"protected_method_#{i}"
    define_method(:"private_method_#{i}") {}
    private :"protected_method_#{i}"
  end
end

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

  puts
  puts "using method_defined? and private_method_defined?"
  puts

  [:public, :protected, :private, :undefined].each do |vis|
    puts "  - #{vis} methods"

    3.times do
      GC.start

      bm.report do
        n.times do |i|
          name = :"#{vis}_method_#{i}"
          Foo.method_defined?(name) || Foo.private_method_defined?(name)
        end
      end
    end
  end

  puts
  puts "using public_method_defined?, protected_method_defined? and private_method_defined?"
  puts

  [:public, :protected, :private, :undefined].each do |vis|
    puts "  - #{vis} methods"

    3.times do
      GC.start

      bm.report do
        n.times do |i|
          name = :"#{vis}_method_#{i}"
          Foo.public_method_defined?(name) ||
          Foo.protected_method_defined?(name)
          Foo.private_method_defined?(name)
        end
      end
    end
  end
end

=begin

1000000 times - ruby 2.0.0

using method_defined? and private_method_defined?

  - public methods
   1.410000   0.040000   1.450000 (  1.462588)
   1.380000   0.000000   1.380000 (  1.372015)
   1.370000   0.000000   1.370000 (  1.372362)
  - protected methods
   1.410000   0.000000   1.410000 (  1.402750)
   1.440000   0.000000   1.440000 (  1.442719)
   1.460000   0.010000   1.470000 (  1.464763)
  - private methods
   1.390000   0.000000   1.390000 (  1.393956)
   1.340000   0.000000   1.340000 (  1.349340)
   1.360000   0.000000   1.360000 (  1.361910)
  - undefined methods
   3.260000   0.050000   3.310000 (  3.316372)
   1.260000   0.010000   1.270000 (  1.266557)
   1.250000   0.000000   1.250000 (  1.248734)

using public_method_defined?, protected_method_defined? and private_method_defined?

  - public methods
   1.550000   0.000000   1.550000 (  1.550655)
   1.540000   0.010000   1.550000 (  1.543906)
   1.540000   0.000000   1.540000 (  1.538267)
  - protected methods
   1.590000   0.000000   1.590000 (  1.598310)
   1.600000   0.000000   1.600000 (  1.595205)
   1.600000   0.000000   1.600000 (  1.604186)
  - private methods
   1.530000   0.000000   1.530000 (  1.530080)
   1.560000   0.000000   1.560000 (  1.562656)
   1.560000   0.000000   1.560000 (  1.569161)
  - undefined methods
   1.300000   0.000000   1.300000 (  1.298066)
   1.310000   0.000000   1.310000 (  1.310737)
   1.290000   0.000000   1.290000 (  1.288307)

=end