昨天review公司代码的时候发现有的同事用String.split()方法来解析字符串, 感觉那样效率很低, 于是做了如下的测试:
[进入即可看到]
不难看出String.split()方法的效率是较低的, 而StringTokenizer跟indexOf()解析效率基本一样.
究其原因, java中对产生数组的消耗是很大的, 尤其当我们不知道数组大小的时候(ArrayList其实就是每次多多分配一部分数组, 而使其速度提升的, StringBuffer亦是如此), 造成资源的浪费更是严重.而StringTokenizer跟indexOf()本质上是一直的, 所有效率也不相上下. 但是如果我们调用了st.countTokens()方法, 那么速度就会跌一大截.
package test1;
import java.util.StringTokenizer;
/**
* @author tzh
*
*/
public class Test008 {
public static final String getTerminalFromYardLocation(String yardLocation) {
String result = null;
if (yardLocation != null && yardLocation.startsWith("Y")) {
int start = yardLocation.indexOf('','') + 1;
int end = yardLocation.indexOf('','', start);
if (end > start) {
result = yardLocation.substring(start, end);
}
}
return result;
}
public static final String getBlockFromYardLocation(String yardLocation) {
String result = null;
if (yardLocation != null && yardLocation.startsWith("Y")) {
int index = 0, blockPosition = 3;
StringBuffer tmp = new StringBuffer();
for (int i = 0; i < yardLocation.length(); i++) {
if (yardLocation.charAt(i) == '','') {
if (index++ > 4) {
break;
}
} else if (index == blockPosition) {
tmp.append(yardLocation.charAt(i));
}
}
result = tmp.toString();
}
return result;
}
public static final String getTest(String test) {
String s = null;
if (test != null && test.startsWith("Y")) {
String[] gt = test.split(",");
if (gt.length > 1) {
s = gt[1];
}
}
return s;
}
public static final String getTest1(String test) {
String s = null;
if (test != null && test.startsWith("Y")) {
String[] gt = test.split(",");
if (gt.length > 2) {
s = gt[2];
}
}
return s;
}
public static final String getTest2(String test) {
String s = null;
if (test != null && test.startsWith("Y")) {
StringTokenizer st = new StringTokenizer(test, ",");
//st.countTokens();
st.nextToken();
s = st.nextToken();
//s = st.nextToken();
}
return s;
}
public static void main(String[] args) {
String s = "Y,T,1,B14,1,2,1,2";
int max = 100000;
long start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
getTerminalFromYardLocation(s);
}
System.out.println("getTerminalFromYardLocation escape time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
getBlockFromYardLocation(s);
}
System.out.println("getBlockFromYardLocation escape time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
getTest(s);
}
System.out.println("getTest escape time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
getTest1(s);
}
System.out.println("getTest1 escape time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
getTest2(s);
}
System.out.println("getTest1 escape time: "
+ (System.currentTimeMillis() - start) + " ms");
}
}
测试结果如下:
getTerminalFromYardLocation escape time: 46 ms
getBlockFromYardLocation escape time: 94 ms
getTest escape time: 985 ms
getTest1 escape time: 875 ms
getTest1 escape time: 93 ms