String, StringBuffer, StringBuilder 举例探究效率

在自己以往的学习及工作经验中,形成的概念:

String对象具有不变性,一旦String对象生成,就不可能在被改变;

StringBuffer 线程安全;

StringBuilder 线程不安全;

 

现针对以上三种Object,在自己机器上手写代码做测试,以验证三者的效率差异:

三种Object同时在3种不同的字符串拼接中,循环5w次,耗时对比:

 

Source Code: Test.java

public class Test {
	
	static int len = 50000;
	
	public static void main(String[] args) {
		System.out.println("---testOne()---");
		testOne();
		System.out.println("---testTwo()---");
		testTwo();
		System.out.println("---testThree()---");
		testThree();
	}

	
	static void testOne() {
		Long t1 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			String result1 = "String" + "and" + "String" + "append";
		}
		Long t2 = System.currentTimeMillis();

		for (int i = 0; i < len; i++) {
			StringBuffer sb = new StringBuffer();
			sb.append("String");
			sb.append("and");
			sb.append("String");
			sb.append("append");

		}
		Long t3 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			StringBuilder sb = new StringBuilder();
			sb.append("String");
			sb.append("and");
			sb.append("String");
			sb.append("append");
		}
		Long t4 = System.currentTimeMillis();
		System.out.println("String:" + (t2 - t1)+"ms");
		System.out.println("StringBuffer:" + (t3 - t2)+"ms");
		System.out.println("StringBuilder:" + (t4 - t3)+"ms");
	}

	static void testTwo() {
		String temp = "abcd";
		String temp2 = "abcd";
		String temp3 = "abcd";
		Long t1 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			String result1 = temp + temp2 + temp3;
		}
		Long t2 = System.currentTimeMillis();

		for (int i = 0; i < len; i++) {
			StringBuffer sb = new StringBuffer();
			sb.append(temp);
			sb.append(temp2);
			sb.append(temp3);

		}
		Long t3 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			StringBuilder sb = new StringBuilder();
			sb.append(temp);
			sb.append(temp2);
			sb.append(temp3);
		}
		Long t4 = System.currentTimeMillis();
		System.out.println("String:" + (t2 - t1)+"ms");
		System.out.println("StringBuffer:" + (t3 - t2)+"ms");
		System.out.println("StringBuilder:" + (t4 - t3)+"ms");
	}

	static void testThree() {
		String temp = "abcd";
		String temp2 = "abcd";
		String temp3 = "abcd";
		Long t1 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			String result1 = "String" + "and" + "String" + "append" + temp
					+ temp2 + temp3;
		}
		Long t2 = System.currentTimeMillis();

		for (int i = 0; i < len; i++) {
			StringBuffer sb = new StringBuffer();
			sb.append("String");
			sb.append("and");
			sb.append("String");
			sb.append("append");
			sb.append(temp);
			sb.append(temp2);
			sb.append(temp3);

		}
		Long t3 = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			StringBuilder sb = new StringBuilder();
			sb.append("String");
			sb.append("and");
			sb.append("String");
			sb.append("append");
			sb.append(temp);
			sb.append(temp2);
			sb.append(temp3);
		}
		Long t4 = System.currentTimeMillis();
		System.out.println("String:" + (t2 - t1)+"ms");
		System.out.println("StringBuffer:" + (t3 - t2)+"ms");
		System.out.println("StringBuilder:" + (t4 - t3)+"ms");

	}
}

 

反编译Test.class

import java.io.PrintStream;

public class Test
{
  static int len = 50000;

  public static void main(String[] args) {
    System.out.println("---testOne()---");
    testOne();
    System.out.println("---testTwo()---");
    testTwo();
    System.out.println("---testThree()---");
    testThree();
  }

  static void testOne()
  {
    Long t1 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      String str = "StringandStringappend";
    }
    Long t2 = Long.valueOf(System.currentTimeMillis());

    for (int i = 0; i < len; i++) {
      StringBuffer sb = new StringBuffer();
      sb.append("String");
      sb.append("and");
      sb.append("String");
      sb.append("append");
    }

    Long t3 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      StringBuilder sb = new StringBuilder();
      sb.append("String");
      sb.append("and");
      sb.append("String");
      sb.append("append");
    }
    Long t4 = Long.valueOf(System.currentTimeMillis());
    System.out.println("String:" + (t2.longValue() - t1.longValue()) + "ms");
    System.out.println("StringBuffer:" + (t3.longValue() - t2.longValue()) + "ms");
    System.out.println("StringBuilder:" + (t4.longValue() - t3.longValue()) + "ms");
  }

  static void testTwo() {
    String temp = "abcd";
    String temp2 = "abcd";
    String temp3 = "abcd";
    Long t1 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      String str1 = temp + temp2 + temp3;
    }
    Long t2 = Long.valueOf(System.currentTimeMillis());

    for (int i = 0; i < len; i++) {
      StringBuffer sb = new StringBuffer();
      sb.append(temp);
      sb.append(temp2);
      sb.append(temp3);
    }

    Long t3 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      StringBuilder sb = new StringBuilder();
      sb.append(temp);
      sb.append(temp2);
      sb.append(temp3);
    }
    Long t4 = Long.valueOf(System.currentTimeMillis());
    System.out.println("String:" + (t2.longValue() - t1.longValue()) + "ms");
    System.out.println("StringBuffer:" + (t3.longValue() - t2.longValue()) + "ms");
    System.out.println("StringBuilder:" + (t4.longValue() - t3.longValue()) + "ms");
  }

  static void testThree() {
    String temp = "abcd";
    String temp2 = "abcd";
    String temp3 = "abcd";
    Long t1 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      String str1 = "StringandStringappend" + temp + 
        temp2 + temp3;
    }
    Long t2 = Long.valueOf(System.currentTimeMillis());

    for (int i = 0; i < len; i++) {
      StringBuffer sb = new StringBuffer();
      sb.append("String");
      sb.append("and");
      sb.append("String");
      sb.append("append");
      sb.append(temp);
      sb.append(temp2);
      sb.append(temp3);
    }

    Long t3 = Long.valueOf(System.currentTimeMillis());
    for (int i = 0; i < len; i++) {
      StringBuilder sb = new StringBuilder();
      sb.append("String");
      sb.append("and");
      sb.append("String");
      sb.append("append");
      sb.append(temp);
      sb.append(temp2);
      sb.append(temp3);
    }
    Long t4 = Long.valueOf(System.currentTimeMillis());
    System.out.println("String:" + (t2.longValue() - t1.longValue()) + "ms");
    System.out.println("StringBuffer:" + (t3.longValue() - t2.longValue()) + "ms");
    System.out.println("StringBuilder:" + (t4.longValue() - t3.longValue()) + "ms");
  }
}

 

测试运行结果:

 

将len设为50w,运行结果差异会更大;

 

总结:

对一个注重执行效率的系统,应该根据实际当中不同的业务需求,按需选用String, StringBuffer 或 StringBuilder。

----------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值