https://kotlinlang.org logo
Title
c

cheeze2000

06/24/2022, 1:55 AM
does anyone know if
b in a..c
has the same performance as
a <= b && b <= c
?
s

Starr

06/24/2022, 1:57 AM
I think it gets optimized into the same bytecode
c

cheeze2000

06/24/2022, 2:01 AM
thank you
i would believe so
c

cheeze2000

06/24/2022, 2:04 AM
oh i see, i would assume it also works for Chars right?
'0'..'9'
i would use the
Char.isDigit()
method but apparently it includes other numeric characters from different languages
e

ephemient

06/24/2022, 2:24 AM
also any Comparable in a different optimization, which of course just eliminates the range, as it still needs the compare() function call
c

cheeze2000

06/24/2022, 2:26 AM
thanks a lot 👍
k

Klitos Kyriacou

06/24/2022, 8:10 AM
Now that one was easy to optimize. How about
c in ('a'..'z') + ('A'..'Z') + ('0'..'9')
?
e

ephemient

06/24/2022, 8:15 AM
no, that results in a
List<Char>
and
.contains
c

cheeze2000

06/24/2022, 8:21 AM
oh i see. so it's better if you do
c in 'a'..'z' || c in 'A'..'Z'
i suppose?
s

Starr

06/24/2022, 8:38 AM
I'm honestly not sure, and I think this starts to get into preoptimization territory -- for 99% of applications it doesn't matter. In any case, I think you could use
Char.isLetter()
(and pass in a Locale if that really matters to you).
k

Klitos Kyriacou

06/24/2022, 8:41 AM
How do we pass in a Locale?
s

Starr

06/24/2022, 8:47 AM
isLetter
has an optional Locale parameter, just pass it in
k

Klitos Kyriacou

06/24/2022, 8:58 AM
s

Starr

06/24/2022, 9:01 AM
Maybe it's in Java's
Character
? idk
🙂 1
r

ribesg

06/24/2022, 10:00 AM
If such optimization really matters I would at least encapsulate it in an inline extension
⬆️ 1