Han Holl wrote:
Yes, like a test for set membership. It seems
intuitive to me.
I’d be more inclined to alias it to the Enumerable#include? myself. That
is, unless someone does use Hash#=== for something as is. (Doesn’t it
just go up to Object#=== and degenerate into a == as it is now?)
But as for your benchmarks:
- hash key lookups are supposed to be fast, with constant lookup
time. This is not surprising. I feel that you are comparing apples to
oranges.
- your tests aren’t completely fair, as the hashobject is
pregenerated, while the others are literals in the case statements
themselves. Doesn’t yield much difference, but still:
require 'benchmark'
include Benchmark
class Hash
alias === has_key?
end
array1 = “abc”, “def”, “kli”, “lop”, “tli”, “ppp”
regexp1 = /^(abc|def|kli|lop|tli|ppp)$/
regexp2 = /^(?:abc|def|kli|lop|tli|ppp)$/
n = 100000
hash = { “abc”=> 1, “def”=> 1, “kli”=> 1, “lop”=> 1, “tli”=> 1, “ppp”=>
1 }
[ “ppp”, “abc”, “for” ].each do |a|
bm(7) do |x|
# first the explicit array version
x.report(a+’ a’){for i in 1…n;
case a
when “abc”, “def”, “kli”, “lop”, “tli”, “ppp”
end
end}
x.report(a){for i in 1…n;
case a
when *array1 #“abc”, “def”, “kli”, “lop”, “tli”, “ppp”
end
end}
x.report(a+’ r1’){for i in 1…n;
case a
when /^(abc|def|kli|lop|tli|ppp)$/
end
end}
x.report(a){for i in 1…n;
case a
when regexp1 #/^(abc|def|kli|lop|tli|ppp)$/
end
end}
x.report(a+’ r2’){for i in 1…n;
case a
when /^(?:abc|def|kli|lop|tli|ppp)$/
end
end}
x.report(a){for i in 1…n;
case a
when regexp2 #/^(?:abc|def|kli|lop|tli|ppp)$/
end
end}
x.report(a+’ h’){for i in 1…n;
case a
when { “abc”=> 1, “def”=> 1, “kli”=> 1, “lop”=> 1, “tli”=> 1,
“ppp”=> 1 }
end
end}
x.report(a){for i in 1…n;
case a
when hash
end
end}
end
end
ppp a 0.640000 0.000000 0.640000 ( 0.645890)
ppp 0.290000 0.000000 0.290000 ( 0.299588)
ppp r1 0.420000 0.010000 0.430000 ( 0.424303)
ppp 0.420000 0.000000 0.420000 ( 0.418180)
ppp r2 0.380000 0.000000 0.380000 ( 0.382281)
ppp 0.380000 0.000000 0.380000 ( 0.382734)
ppp h 1.800000 0.000000 1.800000 ( 1.799063)
ppp 0.130000 0.000000 0.130000 ( 0.128566)
user system total real
abc a 0.170000 0.000000 0.170000 ( 0.167413)
abc 0.110000 0.000000 0.110000 ( 0.109137)
abc r1 0.340000 0.000000 0.340000 ( 0.335180)
abc 0.330000 0.000000 0.330000 ( 0.331492)
abc r2 0.310000 0.000000 0.310000 ( 0.315915)
abc 0.320000 0.000000 0.320000 ( 0.318973)
abc h 1.820000 0.000000 1.820000 ( 1.815141)
abc 0.130000 0.000000 0.130000 ( 0.128977)
user system total real
for a 0.660000 0.000000 0.660000 ( 0.659464)
for 0.300000 0.000000 0.300000 ( 0.296972)
for r1 0.270000 0.000000 0.270000 ( 0.276839)
for 0.280000 0.000000 0.280000 ( 0.277514)
for r2 0.260000 0.000000 0.260000 ( 0.260307)
for 0.260000 0.000000 0.260000 ( 0.261449)
for h 1.790000 0.000000 1.790000 ( 1.795176)
for 0.120000 0.000000 0.120000 ( 0.115688)
Note that pregenerating the regexps doesn’t yield much, but expanding a
pregenerated array seems to have very much effect, even going below the
hash version for ‘abc’. But then that is because the first element of
the array is ‘abc’.
···
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)