Project

General

Profile

Feature #15765 ยป benchmarks.rb

alanwu (Alan Wu), 04/12/2019 04:09 AM

 
1
require 'benchmark'
2

    
3
N = 100_000
4

    
5
Benchmark.bm(60) do |x|
6
  x.report('normal class and module definition') do
7
    N.times do
8
      class A
9
        module B
10
          module C
11
          end
12
        end
13

    
14
        module D
15
        end
16

    
17
        module E
18
          class F
19
          end
20
        end
21
      end
22

    
23
      class Object
24
        remove_const(:A)
25
      end
26
    end
27
  end
28

    
29
  x.report('assign annoymous class to constant after class_eval') do
30
    N.times do
31
      c = Class.new
32
      c.class_eval("module A; end; module B;end")
33
      Klass = c
34
      Object.send(:remove_const, :Klass)
35
    end
36
  end
37

    
38
  [5, 10, 20, 50, 100, 1_000, 10_000, 50000].each do |nesting_level|
39
    x.report("assign structure nested for #{nesting_level} levels to constant") do
40
      c = Class.new
41
      c::DeepNesting = Module.new
42
      current = c::DeepNesting
43
      nesting_level.times do |i|
44
        current::Deeper = Module.new
45
        current = current::Deeper
46
      end
47

    
48
      Klass = c
49
      Object.send(:remove_const, :Klass)
50
    end
51
  end
52
end
53

    
54
<<-BEFORE_PATCH # r67513
55
                                                                   user     system      total        real
56
normal class and module definition                             0.429538   0.000000   0.429538 (  0.430925)
57
assign annoymous class to constant after class_eval            5.587715   0.000000   5.587715 (  5.605187)
58
assign structure nested for 5 levels to constant               0.000025   0.000000   0.000025 (  0.000024)
59
assign structure nested for 10 levels to constant              0.000008   0.000000   0.000008 (  0.000007)
60
assign structure nested for 20 levels to constant              0.000012   0.000000   0.000012 (  0.000011)
61
assign structure nested for 50 levels to constant              0.000026   0.000000   0.000026 (  0.000026)
62
assign structure nested for 100 levels to constant             0.000053   0.000000   0.000053 (  0.000053)
63
assign structure nested for 1000 levels to constant            0.000564   0.000000   0.000564 (  0.000564)
64
assign structure nested for 10000 levels to constant           0.008585   0.000000   0.008585 (  0.008608)
65
BEFORE_PATCH
66

    
67
<<-AFTER_PATCH
68
                                                                   user     system      total        real
69
normal class and module definition                             0.413949   0.003038   0.416987 (  0.417561)
70
assign annoymous class to constant after class_eval            1.364841   0.003332   1.368173 (  1.370387)
71
assign structure nested for 5 levels to constant               0.000012   0.000000   0.000012 (  0.000012)
72
assign structure nested for 10 levels to constant              0.000012   0.000000   0.000012 (  0.000011)
73
assign structure nested for 20 levels to constant              0.000018   0.000000   0.000018 (  0.000019)
74
assign structure nested for 50 levels to constant              0.000069   0.000000   0.000069 (  0.000070)
75
assign structure nested for 100 levels to constant             0.000550   0.000004   0.000554 (  0.000555)
76
assign structure nested for 1000 levels to constant            0.000000   0.002996   0.002996 (  0.003014)
77
assign structure nested for 10000 levels to constant           0.120534   0.076741   0.197275 (  0.198107)
78
AFTER_PATCH