当前位置:网站首页>批量拼接字符串
批量拼接字符串
2022-06-11 07:43:00 【叮当的猫猫】
两种方式
普通的几个字符串拼接成一个字符串,直接使用“+”
从JDK5开始,Java编译器就做了优化,使用“+”拼接字符串,编译器编译后实际就自动优化为使用StringBuilder
创建使用“+”拼接字符串和使用StringBuilder拼接字符串的方法;并新增Junit测试用例,分别调用拼接字符串100000次(这里不是循环拼接,而是执行多次拼接,因为一次拼接耗时太少,看不出差异),打印耗时。
/** * 使用+拼接字符串 */
public String concatenationStringByPlus(String prefix, int i) {
return prefix + "-" + i;
}
/** * 使用StringBuilder拼接字符串 */
public String concatenationStringByStringBuilder(String prefix, int i) {
return new StringBuilder().append(prefix).append("-").append(i).toString();
}
/** * 测试使用+拼接字符串耗时 */
@Test
public void testStringConcatenation01ByPlus() {
long startTime = System.currentTimeMillis();
int count = 100000;
for (int i = 0; i < count; i++) {
String str = concatenationStringByPlus("testStringConcatenation01ByStringBuilder:", i);
}
long endTime = System.currentTimeMillis();
System.out.println("testStringConcatenation01ByPlus,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒");
}
/** * 测试使用StringBuilder拼接字符串耗时 */
@Test
public void testStringConcatenation02ByStringBuilder() {
long startTime = System.currentTimeMillis();
int count = 100000;
for (int i = 0; i < count; i++) {
String str = concatenationStringByStringBuilder("testStringConcatenation02ByStringBuilder:", i);
}
long endTime = System.currentTimeMillis();
System.out.println("testStringConcatenation02ByStringBuilder,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒");
}
testStringConcatenation01ByPlus,拼接字符串100000次,花费33秒
testStringConcatenation02ByStringBuilder,拼接字符串100000次,花费36秒
到class文件所在目录,执行 javap -c StringTest.class,对class文件进行反编译,查看编译后的代码差异。这里不要使用Intellij idea和JD进行反编译,因为反编译有优化,会都反编译成“+”拼接的,看不出来编译后的真正情况。
可以看出两种拼接方法反编译后完全一样,没有差异,执行效率自然也是一样的
循环拼接一个字符串,使用StringBuilder
虽然“+”拼接字符串编译后也会变成StringBuilder,但是每次循环处理都会new一个StringBuilder对象,耗时会大大增加。而直接使用StringBuilder,new一次就可以了,效率相对高
/** * 循环使用+拼接字符串 */
@Test
public void testLoopStringConcatenation03ByPlus() {
long startTime = System.currentTimeMillis();
int count = 10000;
String str = "testLoopStringConcatenation03ByPlus:";
for (int i = 0; i < count; i++) {
str = str + "-" + i;
}
System.out.println(str);
long endTime = System.currentTimeMillis();
System.out.println("testLoopStringConcatenation03ByPlus,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒");
}
/** * 测试循环使用StringBuilder拼接字符串耗时 */
@Test
public void testLoopStringConcatenation04ByStringBuilder() {
long startTime = System.currentTimeMillis();
int count = 100000;
StringBuilder stringBuilder = new StringBuilder("testLoopStringConcatenation04ByStringBuilder:");
for (int i = 0; i < count; i++) {
stringBuilder.append("-");
stringBuilder.append(i);
}
String str = stringBuilder.toString();
System.out.println(str);
long endTime = System.currentTimeMillis();
System.out.println("testLoopStringConcatenation04ByStringBuilder,拼接字符串" + count + "次,花费" + (endTime - startTime) + "秒");
}
testLoopStringConcatenation03ByPlus,拼接字符串10000次,花费463秒
testLoopStringConcatenation04ByStringBuilder,拼接字符串10000次,花费13秒
总结
循环拼接时使用“+”拼接字符串效率较低,推荐使用StringBuilder拼接字符串。
边栏推荐
- 【HDU6357】Hills And Valleys(DP)
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- [atcoder2307] tree game
- 【AtCoder1980】Mysterious Light(数学模拟)
- Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
- 【NOIP2016 D1T3】换教室(期望DP+Floyd)(究极思维陷阱!)
- 零基础自学SQL课程 | UNION 联合查询
- SQLZOO刷题记录-3
- Configuration software -- control drag and drop
- Black Qunhui dsm7.0.1 physical machine installation tutorial
猜你喜欢

Import on CSDN MD file

C wechat upload form data

Use of wordcloud
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?

【软件测试】这样的简历已经刷掉了90%的面试者
![[Oracle database] mammy tutorial day02 use of database management tool sqlplus](/img/f2/8f6f74a62427ebfb4c805c1e9b3352.png)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus

RTMP protocol

2021-11-05 definition of cache

零基础自学SQL课程 | OUTER JOIN外连接

May 30-June 5, 2022 AI industry weekly (issue 100): three years
随机推荐
C language judging big end and small end [consortium or pointer] big end and small end conversion
Use of wordcloud
[atcoder2307] tree game
排序——归并排序
How to prepare for the new PMP syllabus exam?
Analyse du contrat du modèle de taux composé
Nosqlzoo question brushing-1
20200810 T2 dispatch money
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
QT table display data
Introduction to operations research
C language lesson 2
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
VIM common commands
【POJ3691】DNA repair (AC自动机+DP)
Compound RateModel合约解析
【AtCoder2304】Cleaning
Wc2020 guessing game
Sdl-4 PCM playback
【AtCoder1998】Stamp Rally(整体二分+并查集)