这里我选取作为比较的例子是, 在一个url后面拼接参数。
关于拼接参数, 无非就几种,一种就是String的 ‘+’ ,一种就是StringBuilder.append(StringBuffer.append) ,还有就是String.format。
先来看看这三种的效果:
TestStringAppend.java
/** * @Author : JCccc * @CreateTime : 2019/12/5 * @Description : **/ public class TestStringAppend { private static int forTimes=10000; private static String url = "http://localhost:8080/testSomeThings?"; private static String uId = "100180"; private static String userName = "testName"; private static String userPwd = "Nux321mXJc"; private static void testStringFormat() { System.out.println("--------------testStringFormat---------------"); long beginTime = System.currentTimeMillis(); String resultStr = String.format("%suId=%s&userName=%s&userPwd=%s", url, uId, userName, userPwd); System.out.println(resultStr); System.out.println(System.currentTimeMillis() - beginTime); } private static void testStringBuilder() { System.out.println("--------------testStringBuilder---------------"); long beginTime = System.currentTimeMillis(); StringBuilder resultBuilder = new StringBuilder(); resultBuilder.append(url).append("uId=").append(uId).append("&userName=").append(userName) .append("&userPwd=").append(userPwd); String resultStr = resultBuilder.toString(); System.out.println(resultStr); System.out.println(System.currentTimeMillis() - beginTime); } private static void testStringAdd() { System.out.println("--------------testStringAdd---------------"); long beginTime = System.currentTimeMillis(); String resultStr = url + "uId=" + uId + "&userName=" + userName + "&userPwd=" + userPwd; System.out.println(resultStr); System.out.println(System.currentTimeMillis() - beginTime); } public static void main(String[] args) { testStringFormat(); testStringBuilder(); testStringAdd(); } }
效果:
可以看到,单纯一次的拼接下,其实三种方式差异基本可以忽略,没什么区别,时间基本可以忽略不计。
接下来我们在每个方法里面都加入循环,而且加入内存占用计算:
private static void testStringFormatFor() { System.out.println("--------------testStringFormatFor---------------"); Runtime runtime = Runtime.getRuntime(); long memory = runtime.freeMemory(); long beginTime = System.currentTimeMillis(); String resultStr=""; for (int i = 0; i <= forTimes; i++) { resultStr = String.format("%suId=%s&userName=%s&userPwd=%s", url, uId, userName, userPwd); } System.out.println(System.currentTimeMillis() - beginTime); System.out.println((memory - runtime.freeMemory()) / 10000 + "W"); } private static void testStringBuilderFor() { System.out.println("--------------testStringBuilderFor---------------"); Runtime runtime = Runtime.getRuntime(); long memory = runtime.freeMemory(); long beginTime = System.currentTimeMillis(); StringBuilder resultBuilder = new StringBuilder(); for (int i = 0; i <= forTimes; i++) { resultBuilder.append(url).append("uId=").append(uId).append("&userName=").append(userName) .append("&userPwd=").append(userPwd); } System.out.println(System.currentTimeMillis() - beginTime); System.out.println((memory - runtime.freeMemory()) / 10000 + "W"); } private static void testStringAddFor() { System.out.println("--------------testStringAddFor---------------"); Runtime runtime = Runtime.getRuntime(); long memory = runtime.freeMemory(); long beginTime = System.currentTimeMillis(); String resultStr =null; for (int i = 0; i <= forTimes; i++) { resultStr= url + "uId=" + uId + "&userName=" + userName + "&userPwd=" + userPwd; } System.out.println(System.currentTimeMillis() - beginTime); System.out.println((memory - runtime.freeMemory()) / 10000 + "W"); }
这里循环的次数是 1万次,这样就初步区别出差异了:
结论:
StringBuilder 性能最佳,无论在耗时还是耗内存方面。
String的 ‘+’ 拼接,性能接近StringBuilder,因为底层其实就是StringBuilder。
String.format 性能最弱,但是如果是单次的拼接上,代码可读性最佳。
个人感觉,如果不太注重性能,用String.format是很不错的,代码整洁,可读性很高。
但是实际上用‘+’拼接可读性也是很高的,为什么这么说,因为大部分人都是直接用‘+‘ 拼接,看习惯了可读性也就提升了。
其次如果需要优化性能,那不用说了,看完这篇你肯定知道怎么去优化了。