Do all these compile to the same code, and if not, which one is faster?
if (x..y).includes? a
if (x..y).covers? a
if x <= a <= y
Do all these compile to the same code, and if not, which one is faster?
if (x..y).includes? a
if (x..y).covers? a
if x <= a <= y
They’re the same.
#includes? is one less call, but this is likely optimised out during a build. It’s one of the few (/only?) methods in stdlib with dual names.
Interesting.
I wonder if this covers? “alias” was introduced before “we discourage aliases in Crystal” altitude?
I’ve tried these in Compiler Explorer where Crystal support was added by @HertzDevil recently.
Add --release flag in Compiler Options:
(1..3).includes? 2
(1..3).covers? 2
1 <= 2 <= 3
And they seem to produce exactly the same code.
Without --release flag they generate different code though, so covers? would be 1 more method call and plain conditions would be no method calls at all.
Thanks all. In Ruby cover? and include? are a little bit different.