/**
* @author Tan Wenzheng
*/
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class TestString {
private static final int max = 100;
public void testPlus() {
System.out.println(“>>> testPlus() <<<”);
String str = “”;
long start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
str = str + “a”;
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {str + “a”} cost=" + cost + " ms");
}
public void testConcat() {
System.out.println(“>>> testConcat() <<<”);
String str = “”;
long start = System.currentTimeMillis();
for (int i = 0; i < max; i++) {
str = str.concat(“a”);
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {str.concat(“a”)} cost=" + cost + " ms");
}
public void testJoin() {
System.out.println(“>>> testJoin() <<<”);
long start = System.currentTimeMillis();
List list = new ArrayList();
for (int i = 0; i < max; i++) {
list.add(“a”);
}
long end1 = System.currentTimeMillis();
long cost1 = end1 - start;
StringUtils.join(list, “”);
long end = System.currentTimeMillis();
long cost = end - end1;
System.out.println(" {list.add(“a”)} cost1=" + cost1 + " ms");
System.out.println(" {StringUtils.join(list, “”)} cost=" + cost
+ " ms");
}
public void testStringBuffer() {
System.out.println(“>>> testStringBuffer() <<<”);
long start = System.currentTimeMillis();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < max; i++) {
strBuffer.append(“a”);
}
strBuffer.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {strBuffer.append(“a”)} cost=" + cost + " ms");
}
public void testStringBuilder() {
System.out.println(“>>> testStringBuilder() <<<”);
long start = System.currentTimeMillis();
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < max; i++) {
strBuilder.append(“a”);
}
strBuilder.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out
.println(" {strBuilder.append(“a”)} cost=" + cost + " ms");
}
}
> 测试结果:
- 执行100次, private static final int max = 100;
testPlus() <<<
{str + “a”} cost=0 ms
testConcat() <<<
{str.concat(“a”)} cost=0 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=20 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行1000次, private static final int max = 1000;
testPlus() <<<
{str + “a”} cost=10 ms
testConcat() <<<
{str.concat(“a”)} cost=0 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=20 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行1万次, private static final int max = 10000;
testPlus() <<<
{str + “a”} cost=150 ms
testConcat() <<<
{str.concat(“a”)} cost=70 ms
testJoin() <<<
{list.add(“a”)} cost1=0 ms
{StringUtils.join(list, “”)} cost=30 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=0 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=0 ms
- 执行10万次, private static final int max = 100000;
testPlus() <<<
{str + “a”} cost=4198 ms
testConcat() <<<
{str.concat(“a”)} cost=1862 ms
testJoin() <<<
{list.add(“a”)} cost1=21 ms
{StringUtils.join(list, “”)} cost=49 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=10 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=10 ms
- 执行20万次, private static final int max = 200000;
testPlus() <<<
{str + “a”} cost=17196 ms
testConcat() <<<
{str.concat(“a”)} cost=7653 ms
testJoin() <<<
{list.add(“a”)} cost1=20 ms
{StringUtils.join(list, “”)} cost=51 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=20 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=16 ms
- 执行50万次, private static final int max = 500000;
testPlus() <<<
{str + “a”} cost=124693 ms
testConcat() <<<
{str.concat(“a”)} cost=49439 ms
testJoin() <<<
{list.add(“a”)} cost1=21 ms
{StringUtils.join(list, “”)} cost=50 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=20 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=10 ms
- 执行90万次, private static final int max = 900000;
testPlus() <<<
{str + “a”} cost=456739 ms
testConcat() <<<
{str.concat(“a”)} cost=186252 ms
testJoin() <<<
{list.add(“a”)} cost1=20 ms
{StringUtils.join(list, “”)} cost=68 ms
testStringBuffer() <<<
{strBuffer.append(“a”)} cost=30 ms
testStringBuilder() <<<
{strBuilder.append(“a”)} cost=24 ms
> 查看源代码,以及简单分析
言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
搜集费时费力,能看到此处的都是真爱!
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
[外链图片转存中…(img-bYOihCp2-1714355999831)]
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
[外链图片转存中…(img-e0vuHZX3-1714355999832)]
搜集费时费力,能看到此处的都是真爱!