Project

General

Profile

Feature #15281 ยป intersect_standalone.rb

RGBD (Oleg Zubchenko), 11/03/2018 12:58 PM

 
1
#!/usr/bin/env ruby
2

    
3
require 'benchmark/ips'
4
require 'set'
5

    
6
n_values1 = 200
7
n_values2 = (n_values1 * 0.8).to_i
8
overlap = (n_values2 * 0.5).to_i
9

    
10
a1 = (0...n_values1).to_a
11
a2 = ((n_values1 - overlap)...(n_values1 - overlap + n_values2)).to_a
12
s1 = Set.new(a1)
13
s2 = Set.new(a2)
14
ss1 = SortedSet.new(a1)
15
ss2 = SortedSet.new(a2)
16

    
17
puts RUBY_VERSION
18
puts s1.size
19
puts s2.size
20
puts((s1 & s2).size)
21

    
22
methods = {
23
  Set_Set: -> { s2 & s1 },
24
  Set_SortedSet: -> { s2 & ss1 },
25
  SortedSet_Set: -> { ss2 & s1 },
26
  SortedSet_SortedSet: -> { ss2 & ss1 },
27
  Set_Array: -> { s2 & a1 },
28
}
29

    
30
Benchmark.ips do |bm|
31
  methods.each do |name, method|
32
    bm.report(name) { method.call }
33
  end
34
end