File: bench_subclasses.rb

package info (click to toggle)
jruby 9.4.12.0%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 89,468 kB
  • sloc: ruby: 550,612; java: 276,882; yacc: 25,873; ansic: 6,285; xml: 6,172; sh: 1,775; sed: 94; makefile: 76; jsp: 48; tcl: 40; exp: 12
file content (44 lines) | stat: -rw-r--r-- 1,050 bytes parent folder | download
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
require 'benchmark/ips'

Custom = Class.new(Object)
# create these once and hold references
SINGLETON_COUNT = 1000
many_singletons = SINGLETON_COUNT.times.map { c = Custom.new; class << c; end; c }

Benchmark.ips do |bm|
  [1, 5, 10, 50].each do |count|
    bm.report("#{count} thread Numeric.subclasses") do
      count.times.map {
        Thread.new {
          i = 10_000 / count
          while i > 0
            Numeric.subclasses
            i-=1
          end
        }
      }.each(&:join)
    end
    bm.report("#{count} thread Object.subclasses") do
      count.times.map {
        Thread.new {
          i = 10_000 / count
          while i > 0
            Object.subclasses
            i-=1
          end
        }
      }.each(&:join)
    end
    bm.report("#{count} thread Custom.subclasses with #{SINGLETON_COUNT} singletons") do
      count.times.map {
        Thread.new {
          i = 10_000 / count
          while i > 0
            Custom.subclasses
            i-=1
          end
        }
      }.each(&:join)
    end
  end
end