Project

General

Profile

bench.rb

Benchmark for iseq scanning - tenderlovemaking (Aaron Patterson), 01/20/2018 02:50 AM

 
1
require "coverage"
2
require "benchmark/ips"
3

    
4
def make_iseq_of_size goal, code
5
  builder = ->(r) {
6
    ruby = <<-eoruby
7
x = 1
8
#{r.times.map { code }.join "\n" }
9
    eoruby
10
    RubyVM::InstructionSequence.compile ruby
11
  }
12

    
13
  low = 1
14
  high = builder.(goal).size
15
  iseq = nil
16

    
17
  while low < high
18
    mid = low + (high - low) / 2
19
    iseq = builder.(mid)
20
    if iseq.size < goal
21
      low = mid + 1
22
    else iseq.size > goal
23
      high = mid - 1
24
    end
25
  end
26

    
27
  iseq
28
end
29

    
30
def make_fast_iseq size
31
  make_iseq_of_size size, 'x + x'
32
end
33

    
34
def make_slow_iseq size
35
  make_iseq_of_size size, 'x = 1'
36
end
37

    
38
res = Benchmark.ips do |x|
39
  fast_iseq_100 = make_fast_iseq(100)
40
  x.report("#{fast_iseq_100.size} fast") { fast_iseq_100.markable_object_count }
41

    
42
  fast_iseq_1000 = make_fast_iseq(1000)
43
  x.report("#{fast_iseq_1000.size} fast") { fast_iseq_1000.markable_object_count }
44

    
45
  fast_iseq_10000 = make_fast_iseq(10000)
46
  x.report("#{fast_iseq_10000.size} fast") { fast_iseq_10000.markable_object_count }
47

    
48
  fast_iseq_100000 = make_fast_iseq(100000)
49
  x.report("#{fast_iseq_100000.size} fast") { fast_iseq_100000.markable_object_count }
50

    
51
  slow_iseq_100 = make_slow_iseq(100)
52
  x.report("#{slow_iseq_100.size} slow") { slow_iseq_100.markable_object_count }
53

    
54
  slow_iseq_1000 = make_slow_iseq(1000)
55
  x.report("#{slow_iseq_1000.size} slow") { slow_iseq_1000.markable_object_count }
56

    
57
  slow_iseq_10000 = make_slow_iseq(10000)
58
  x.report("#{slow_iseq_10000.size} slow") { slow_iseq_10000.markable_object_count }
59

    
60
  slow_iseq_100000 = make_slow_iseq(100000)
61
  x.report("#{slow_iseq_100000.size} slow") { slow_iseq_100000.markable_object_count }
62
end
63

    
64
require 'csv'
65

    
66
CSV do |csv|
67
  csv << ["pointers", "ips"]
68
  res.entries.each do |entry|
69
    csv << [entry.label[/^\d+/], entry.ips]
70
  end
71
end
72

    
73
__END__
74

75
[aaron@TC ruby (eliminate-mark-ary)]$ ./ruby -I ~/git/benchmark-ips/lib x.rb
76
Warming up --------------------------------------
77
            107 fast    51.019k i/100ms
78
            987 fast     6.558k i/100ms
79
           9987 fast   681.000  i/100ms
80
          99987 fast    68.000  i/100ms
81
             98 slow    35.413k i/100ms
82
            998 slow     3.895k i/100ms
83
          10004 slow   397.000  i/100ms
84
          99998 slow    39.000  i/100ms
85
Calculating -------------------------------------
86
            107 fast    592.822k (± 4.3%) i/s -      2.959M in   5.002552s
87
            987 fast     67.417k (± 3.5%) i/s -    341.016k in   5.064936s
88
           9987 fast      6.671k (± 5.4%) i/s -     33.369k in   5.020232s
89
          99987 fast    653.789  (± 8.3%) i/s -      3.264k in   5.032978s
90
             98 slow    378.918k (±10.4%) i/s -      1.912M in   5.113554s
91
            998 slow     39.021k (± 5.3%) i/s -    194.750k in   5.007117s
92
          10004 slow      3.894k (± 5.3%) i/s -     19.850k in   5.112539s
93
          99998 slow    387.217  (± 6.7%) i/s -      1.950k in   5.065411s
94
pointers,ips
95
107,592822.1313505468
96
987,67416.51114832592
97
9987,6671.08318754785
98
99987,653.7886910885874
99
98,378918.2037573504
100
998,39021.06843464474
101
10004,3894.3530685987253
102
99998,387.21668397168526