You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
##### `raise` vs `E2MM#Raise` for raising (and defining) exceptions [code](code/general/raise-vs-e2mmap.rb)[custom exception code](code/general/raise-custom-vs-e2mmap.rb)
138
179
139
180
Ruby's [Exception2MessageMapper module](http://ruby-doc.org/stdlib-2.2.0/libdoc/e2mmap/rdoc/index.html) allows one to define and raise exceptions with predefined messages.
@@ -495,6 +536,70 @@ Comparison:
495
536
Array#sort_by &:-@: 229323.6 i/s - 2.44x slower
496
537
```
497
538
539
+
##### Subset check: `(a1 - a2).empty?` vs alternatives [code](code/array/subset-check.rb)
540
+
541
+
> To check whether every element of `a1` is also in `a2`, `(a1 - a2).empty?` is
542
+
> consistently the fastest across modern Ruby versions for the common case where
543
+
> `a1` is actually a subset. <br>
544
+
> **Caveat:** the winner is highly data-dependent. `a1.all? { |e| a2.include?(e) }`
545
+
> short-circuits on the first miss, so it wins when `a1` is *not* a subset, but it
546
+
> is O(n*m) and degrades badly on large true subsets. <br>
547
+
> Note also that `Set#subset?` (including the `to_set` conversion) was ~6.8x slower
548
+
> on Ruby 3.3/3.4 but only ~1.7x slower on Ruby 4.0, where `Set` became much
549
+
> faster. If you already hold `Set`s (or check repeatedly), `Set#subset?` scales
##### `Date.iso8601` vs `Date.parse`[code](code/date/iso8601-vs-parse.rb)
@@ -896,6 +1021,48 @@ Comparison:
896
1021
Hash#values.include?: 62052.8 i/s - 1.15x slower
897
1022
```
898
1023
1024
+
##### `Hash#values.compact` instead of `Hash#values.select` or `Hash#select.values` (to get non-nil values) [code](code/hash/select-values-vs-values-select-vs-values-compact.rb)
1025
+
1026
+
> To collect the non-nil values of a hash, `Hash#select { |_k, v| !v.nil? }.values` allocates an intermediate hash before extracting its values; <br>
1027
+
> `Hash#values.select { |v| !v.nil? }` skips the intermediate hash but still runs a block per element; <br>
1028
+
> `Hash#values.compact` drops the nils in C without a Ruby-level block, which is fastest.
# Check whether ARRAY1 is a subset of ARRAY2 (every element of ARRAY1 is in ARRAY2).
5
+
# The fastest approach depends on the input: `all?` + `include?` stops at the first miss (great for non-subsets) but is O(n*m) when ARRAY1 really is a subset, while Set-based lookups stay O(n).
6
+
ARRAY1=[*1..25]
7
+
ARRAY2=[*1..100]
8
+
9
+
# On CRuby 3.4 and newer (a1 - a2).empty? wins; on JRuby and TruffleRuby all? + include? does, and on 2.1 the two tie.
10
+
deffaster
11
+
(ARRAY1 - ARRAY2).empty?
12
+
end
13
+
14
+
deffast
15
+
ARRAY1.all?{ |element| ARRAY2.include?(element)}
16
+
end
17
+
18
+
defslow
19
+
(ARRAY1 & ARRAY2).size == ARRAY1.size
20
+
end
21
+
22
+
defslower
23
+
(ARRAY1 & ARRAY2) == ARRAY1
24
+
end
25
+
26
+
defslowest
27
+
ARRAY1.to_set.subset?(ARRAY2.to_set)
28
+
end
29
+
30
+
# Sanity check: every approach must return the same answer.
0 commit comments