String.split()效率

本文通过实验展示了Java中不同字符串解析方法的效率差异,包括split()、StringTokenizer和indexOf()。特别关注了数组生成带来的性能损耗,并探讨了在特定场景下如何选择更高效的解析策略。
摘要由CSDN通过智能技术生成

昨天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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值