Project

General

Profile

Bug #140 ยป set_bench.rb

NoKarma (Arthur Schreiber), 06/11/2008 09:03 AM

 
1
require "set"
2
require "benchmark"
3

    
4
set1 = Set[*(0..1000).to_a]
5
set2 = Set[*(0..1000).to_a]
6

    
7
sset1 = SortedSet[*(0..1000).to_a]
8
sset2 = SortedSet[*(0..1000).to_a]
9

    
10
puts "Without patch:"
11
puts ""
12
puts "Set#== with Set:             #{Benchmark.measure { 300.times { set1 == set2 } }}"
13
puts "Set#== with SortedSet:       #{Benchmark.measure { 300.times { set1 == sset1 } }}"
14
puts "SortedSet#== with SortedSet: #{Benchmark.measure { 300.times { sset1 == sset2 } }}"
15

    
16
class Set
17
  def ==(other)
18
    if self.equal?(other)
19
      true
20
    elsif other.instance_of?(self.class)
21
      @hash == other.instance_variable_get(:@hash)
22
    elsif other.is_a?(Set) && self.size == other.size
23
      other.all? { |o| @hash.include?(o) }
24
    else
25
      false
26
    end
27
  end
28
end
29

    
30
puts ""
31
puts "With patch:"
32
puts ""
33
puts "Set#== with Set:             #{Benchmark.measure { 300.times { set1 == set2 } }}"
34
puts "Set#== with SortedSet:       #{Benchmark.measure { 300.times { set1 == sset1 } }}"
35
puts "SortedSet#== with SortedSet: #{Benchmark.measure { 300.times { sset1 == sset2 } }}"