Skip to content

Instantly share code, notes, and snippets.

@hengyunabc
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengyunabc/a4651e90db24cc5ed29a to your computer and use it in GitHub Desktop.
Save hengyunabc/a4651e90db24cc5ed29a to your computer and use it in GitHub Desktop.
TrimAllWhitespace Test. Spring StringUtils.trimAllWhitespace() is slow.
public class Test {
static public String myTrimAllWhitespace(String str) {
if (str != null) {
int len = str.length();
if (len > 0) {
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (!Character.isWhitespace(c)) {
sb.append(c);
}
}
return sb.toString();
}
}
return str;
}
static public String myTrimAllWhitespace2(String str) {
if (str != null) {
int len = str.length();
if (len > 0) {
char[] dest = new char[len];
int destPos = 0;
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (!Character.isWhitespace(c)) {
dest[destPos++] = c;
}
}
return new String(dest, 0, destPos);
}
}
return str;
}
static public String myTrimAllWhitespace3(String str) {
if (str != null) {
int len = str.length();
if (len > 0) {
char[] src = str.toCharArray();
char[] dest = new char[src.length];
int destPos = 0;
for (int pos1 = 0, pos2 = 0; pos2 < src.length;) {
if (Character.isWhitespace(src[pos2])) {
if (pos1 == pos2) {
pos1++;
pos2++;
} else {
System.arraycopy(src, pos1, dest, destPos, pos2
- pos1);
destPos += (pos2 - pos1);
pos2++;
pos1 = pos2;
}
} else {
pos2++;
}
if (pos2 == src.length) {
if (pos1 != pos2) {
System.arraycopy(src, pos1, dest, destPos, pos2
- pos1);
destPos += (pos2 - pos1);
}
return new String(dest, 0, destPos);
}
}
}
}
return str;
}
public static boolean hasLength(CharSequence str) {
return (str != null && str.length() > 0);
}
static public String myTrimAllWhitespaceUnicode(String str) {
if (!hasLength(str)) {
return str;
}
int pos = 0;
int len = str.length();
StringBuilder sb = new StringBuilder(len);
while (pos < len) {
int codePoint = Character.codePointAt(str, pos);
char[] chars = Character.toChars(codePoint);
if (!Character.isWhitespace(codePoint)) {
sb.append(chars);
}
pos += chars.length;
}
return sb.toString();
}
public static void test() {
String[] strArray = { "", "abc ", " x h z ", " ", " a", "a",
" a ", "a ", " a", " ab cd ef kj ",
"abc edfg oooo ssss", "a a a a " };
for (String string : strArray) {
String trimAllWhitespace = org.springframework.util.StringUtils.trimAllWhitespace(string);
if(!trimAllWhitespace.equals(myTrimAllWhitespace2(string))
|| !trimAllWhitespace.equals(myTrimAllWhitespace3(string))
|| !trimAllWhitespace.equals(myTrimAllWhitespaceUnicode(string))) {
System.err.println(string);
System.err.println("error!");
System.exit(-1);
}
}
}
public static void main(String[] args) {
test();
String[] strArray = {
"ab s df ff ds",
"absdfffffffffffffffffffffffffffffffffffffffff sdfffffffffffffffffffffffffffffffffffffffffs",
"a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a ",
};
int ttt = 0;
for(String string : strArray) {
for (int time = 0; time < 2; ++time) {
System.out.println("----------------------");
System.out.println("string: " + string);
int count = 10000000;
{
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
String replaceBlank = org.springframework.util.StringUtils
.trimAllWhitespace(string);
if (replaceBlank.startsWith("X")) {
ttt += 1;
}
}
long end = System.nanoTime();
System.out.println("trimAllWhitespace, time:\t" + (end - start) / 1000000.0);
}
{
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
String replaceBlank = myTrimAllWhitespace(string);
if (replaceBlank.startsWith("X")) {
ttt += 1;
}
}
long end = System.nanoTime();
System.out.println("myTrimAllWhitespace time:\t" + (end - start) / 1000000.0);
}
{
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
String replaceBlank = myTrimAllWhitespace2(string);
if (replaceBlank.startsWith("X")) {
ttt += 1;
}
}
long end = System.nanoTime();
System.out.println("myTrimAllWhitespace2 time:\t" + (end - start) / 1000000.0);
}
{
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
String replaceBlank = myTrimAllWhitespace3(string);
if (replaceBlank.startsWith("X")) {
ttt += 1;
}
}
long end = System.nanoTime();
System.out.println("myTrimAllWhitespace3 time:\t" + (end - start) / 1000000.0);
}
{
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
String replaceBlank = myTrimAllWhitespaceUnicode(string);
if (replaceBlank.startsWith("X")) {
ttt += 1;
}
}
long end = System.nanoTime();
System.out.println("myTrimAllWhitespaceUnicode time:\t" + (end - start) / 1000000.0);
}
}
}
System.out.println("ttt:" + ttt);
}
}
@hengyunabc
Copy link
Author


string: ab s df ff ds
trimAllWhitespace, time: 1464.930011
myTrimAllWhitespace time: 931.668284
myTrimAllWhitespace2 time: 612.377962
myTrimAllWhitespace3 time: 1030.264558
myTrimAllWhitespaceUnicode time: 1718.113187


string: ab s df ff ds
trimAllWhitespace, time: 1286.266281
myTrimAllWhitespace time: 901.231429
myTrimAllWhitespace2 time: 620.1201
myTrimAllWhitespace3 time: 1089.587691
myTrimAllWhitespaceUnicode time: 1704.805074


string: absdfffffffffffffffffffffffffffffffffffffffff sdfffffffffffffffffffffffffffffffffffffffffs
trimAllWhitespace, time: 2426.387988
myTrimAllWhitespace time: 4370.659633
myTrimAllWhitespace2 time: 2193.304575
myTrimAllWhitespace3 time: 3124.781604
myTrimAllWhitespaceUnicode time: 11450.922751


string: absdfffffffffffffffffffffffffffffffffffffffff sdfffffffffffffffffffffffffffffffffffffffffs
trimAllWhitespace, time: 2435.480225
myTrimAllWhitespace time: 4359.093717
myTrimAllWhitespace2 time: 2259.316877
myTrimAllWhitespace3 time: 3122.034291
myTrimAllWhitespaceUnicode time: 11640.463152


string: a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
trimAllWhitespace, time: 17301.360847
myTrimAllWhitespace time: 6411.480031
myTrimAllWhitespace2 time: 3138.645121
myTrimAllWhitespace3 time: 10339.538524
myTrimAllWhitespaceUnicode time: 15667.220536


string: a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
trimAllWhitespace, time: 17401.499154
myTrimAllWhitespace time: 6504.075594
myTrimAllWhitespace2 time: 3161.379871
myTrimAllWhitespace3 time: 10442.165035
myTrimAllWhitespaceUnicode time: 15365.947787

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment