[
https://issues.apache.org/jira/browse/LANG-604?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12845578#action_12845578
]
Kai Gülzau commented on LANG-604:
---------------------------------
It's a heuristic to guess the position of a non whitespace character.
For example the algorithm is ~50% faster for the
string " 123" and ~100% for the string " 12".
I belive most of the strings checked by isBlank() which
contain whitespaces are untrimmed strings.
So this works for me and should work in general.
You loose ~1-5% performance on blank or random strings
due to computing the middle position of the string.
To limit the performance loss to 50% for strings like "12 "
i've put both loops into one loop. For blank strings with
an odd length the middle char is checked twice.
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0)
return true;
for (int m = l / 2, i = 0; m < l; m++, i++)
if (!Character.isWhitespace(s.charAt(m)) ||
!Character.isWhitespace(s.charAt(i)))
return false;
return true;
}
regards,
Kai Gülzau
> Optimize isBlank() for untrimmed strings
> ----------------------------------------
>
> Key: LANG-604
> URL: https://issues.apache.org/jira/browse/LANG-604
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Affects Versions: 3.0
> Reporter: Kai Gülzau
> Priority: Minor
>
> Change isBlank() to start iteration in the middle of the String.
> So you get better performance for untrimmed Strings like " dummy ".
> Here is my proposal:
> public static boolean isBlank(CharSequence cs) {
> int strLen;
> if (cs == null || (strLen = cs.length()) == 0) {
> return true;
> }
> int mid = strLen / 2, i = mid;
> for (; i < strLen; i++) {
> if (!Character.isWhitespace(cs.charAt(i))) {
> return false;
> }
> }
> for (i = 0; i < mid; i++) {
> if (!Character.isWhitespace(cs.charAt(i))) {
> return false;
> }
> }
> return true;
> }
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.