File: failing_with_distinct_items.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (147 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download | duplicates (6)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
$LOAD_PATH.unshift "./lib"
require 'benchmark'
require 'rspec/expectations'
require 'securerandom'

extend RSpec::Matchers

sizes = [10, 100, 1000, 2000, 4000]

puts "rspec-expectations #{RSpec::Expectations::Version::STRING} -- #{RUBY_ENGINE}/#{RUBY_VERSION}"

puts
puts "Failing `match_array` expectation with lists of distinct strings having 1 unmatched pair"
puts

Benchmark.benchmark do |bm|
  sizes.each do |size|
    actual = Array.new(size) { SecureRandom.uuid }

    expecteds = Array.new(3) do
      array = actual.shuffle
      # replace one entry with a different value
      array[rand(array.length)] = SecureRandom.uuid
      array
    end

    expecteds.each do |expected|
      bm.report("#{size.to_s.rjust(5)} items") do
        begin
          expect(actual).to match_array(expected)
        rescue RSpec::Expectations::ExpectationNotMetError
        else
          raise "did not fail but should have"
        end
      end
    end
  end
end

__END__

Before new composable matchers algo:

   10 items  0.000000   0.000000   0.000000 (  0.000813)
   10 items  0.000000   0.000000   0.000000 (  0.000099)
   10 items  0.000000   0.000000   0.000000 (  0.000127)
  100 items  0.000000   0.000000   0.000000 (  0.000707)
  100 items  0.000000   0.000000   0.000000 (  0.000612)
  100 items  0.000000   0.000000   0.000000 (  0.000600)
 1000 items  0.040000   0.000000   0.040000 (  0.038679)
 1000 items  0.040000   0.000000   0.040000 (  0.041379)
 1000 items  0.040000   0.000000   0.040000 (  0.036680)
 2000 items  0.130000   0.000000   0.130000 (  0.131681)
 2000 items  0.120000   0.000000   0.120000 (  0.123664)
 2000 items  0.130000   0.000000   0.130000 (  0.128799)
 4000 items  0.490000   0.000000   0.490000 (  0.489446)
 4000 items  0.510000   0.000000   0.510000 (  0.511915)
 4000 items  0.480000   0.010000   0.490000 (  0.477616)

After:

   10 items  0.000000   0.000000   0.000000 (  0.001382)
   10 items  0.000000   0.000000   0.000000 (  0.000156)
   10 items  0.000000   0.000000   0.000000 (  0.000161)
  100 items  0.010000   0.000000   0.010000 (  0.005052)
  100 items  0.000000   0.000000   0.000000 (  0.004991)
  100 items  0.010000   0.000000   0.010000 (  0.004984)
 1000 items  0.470000   0.000000   0.470000 (  0.470043)
 1000 items  0.500000   0.000000   0.500000 (  0.499316)
 1000 items  0.490000   0.000000   0.490000 (  0.488582)
 2000 items  1.910000   0.000000   1.910000 (  1.917279)
 2000 items  1.930000   0.010000   1.940000 (  1.931002)
 2000 items  1.920000   0.000000   1.920000 (  1.928989)
 4000 items  7.860000   0.010000   7.870000 (  7.881995)
 4000 items  7.980000   0.010000   7.990000 (  8.003643)
 4000 items  8.000000   0.010000   8.010000 (  8.031382)

With "smaller subproblem" optimization: (about 25% slower)

   10 items  0.010000   0.000000   0.010000 (  0.001331)
   10 items  0.000000   0.000000   0.000000 (  0.000175)
   10 items  0.000000   0.000000   0.000000 (  0.000165)
  100 items  0.000000   0.000000   0.000000 (  0.006137)
  100 items  0.010000   0.000000   0.010000 (  0.005880)
  100 items  0.000000   0.000000   0.000000 (  0.005950)
 1000 items  0.630000   0.000000   0.630000 (  0.634294)
 1000 items  0.620000   0.000000   0.620000 (  0.622427)
 1000 items  0.640000   0.000000   0.640000 (  0.641505)
 2000 items  2.420000   0.000000   2.420000 (  2.419876)
 2000 items  2.430000   0.000000   2.430000 (  2.442544)
 2000 items  2.380000   0.010000   2.390000 (  2.385106)
 4000 items  9.780000   0.010000   9.790000 (  9.811499)
 4000 items  9.670000   0.010000   9.680000 (  9.688799)
 4000 items  9.710000   0.010000   9.720000 (  9.743054)

With "implement `values_match?` ourselves" optimization: (more than twice as fast!)

   10 items  0.000000   0.000000   0.000000 (  0.001189)
   10 items  0.000000   0.000000   0.000000 (  0.000149)
   10 items  0.000000   0.000000   0.000000 (  0.000130)
  100 items  0.000000   0.000000   0.000000 (  0.002927)
  100 items  0.000000   0.000000   0.000000 (  0.002856)
  100 items  0.010000   0.000000   0.010000 (  0.003028)
 1000 items  0.250000   0.000000   0.250000 (  0.245146)
 1000 items  0.240000   0.000000   0.240000 (  0.246291)
 1000 items  0.320000   0.000000   0.320000 (  0.315192)
 2000 items  1.120000   0.000000   1.120000 (  1.128162)
 2000 items  1.030000   0.000000   1.030000 (  1.034982)
 2000 items  1.060000   0.000000   1.060000 (  1.063870)
 4000 items  4.530000   0.000000   4.530000 (  4.556346)
 4000 items  4.400000   0.010000   4.410000 (  4.414447)
 4000 items  4.410000   0.000000   4.410000 (  4.417440)

With e === a || a == e || values_match?(e,a)
   10 items  0.000000   0.000000   0.000000 (  0.001466)
   10 items  0.000000   0.000000   0.000000 (  0.000258)
   10 items  0.000000   0.000000   0.000000 (  0.000251)
  100 items  0.020000   0.000000   0.020000 (  0.012369)
  100 items  0.010000   0.000000   0.010000 (  0.012345)
  100 items  0.010000   0.000000   0.010000 (  0.012744)
 1000 items  1.180000   0.000000   1.180000 (  1.187754)
 1000 items  1.200000   0.000000   1.200000 (  1.198681)
 1000 items  1.210000   0.000000   1.210000 (  1.210159)
 2000 items  4.760000   0.000000   4.760000 (  4.764911)
 2000 items  4.760000   0.000000   4.760000 (  4.757022)
 2000 items  4.760000   0.000000   4.760000 (  4.771776)
 4000 items 19.070000   0.010000  19.080000 ( 19.077930)
 4000 items 19.090000   0.010000  19.100000 ( 19.104171)
 4000 items 19.260000   0.010000  19.270000 ( 19.289653)

With values_match?(e,a)

   10 items  0.000000   0.000000   0.000000 (  0.001462)
   10 items  0.000000   0.000000   0.000000 (  0.000253)
   10 items  0.000000   0.000000   0.000000 (  0.000244)
  100 items  0.010000   0.000000   0.010000 (  0.011913)
  100 items  0.010000   0.000000   0.010000 (  0.011858)
  100 items  0.020000   0.000000   0.020000 (  0.011992)
 1000 items  1.210000   0.000000   1.210000 (  1.226960)
 1000 items  1.140000   0.000000   1.140000 (  1.147002)
 1000 items  1.180000   0.000000   1.180000 (  1.194010)
 2000 items  4.690000   0.010000   4.700000 (  4.740503)
 2000 items  4.680000   0.000000   4.680000 (  4.676084)
 2000 items  4.570000   0.000000   4.570000 (  4.581262)
 4000 items 18.450000   0.010000  18.460000 ( 18.532578)
 4000 items 18.400000   0.010000  18.410000 ( 18.520454)
 4000 items 18.490000   0.020000  18.510000 ( 18.592491)