当前位置:网站首页>Batch splice string
Batch splice string
2022-06-11 07:50:00 【Jingling cat】
Two ways
Common strings are spliced into a single string , Use it directly “+”
from JDK5 Start ,Java The compiler is optimized , Use “+” String concatenation , After compiling, the compiler will automatically optimize to use StringBuilder
Create using “+” String concatenation And use StringBuilder String concatenation Methods ; and newly added Junit The test case , , respectively, Call the concatenation string 100000 Time ( here Not a circular splice , Instead, perform multiple splices , Because one time splicing takes too little time , No difference can be seen ), Printing time .
/** * Use + String concatenation */
public String concatenationStringByPlus(String prefix, int i) {
return prefix + "-" + i;
}
/** * Use StringBuilder String concatenation */
public String concatenationStringByStringBuilder(String prefix, int i) {
return new StringBuilder().append(prefix).append("-").append(i).toString();
}
/** * Test use + Splicing strings takes time */
@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, String concatenation " + count + " Time , cost " + (endTime - startTime) + " second ");
}
/** * Test use StringBuilder Splicing strings takes time */
@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, String concatenation " + count + " Time , cost " + (endTime - startTime) + " second ");
}
testStringConcatenation01ByPlus, String concatenation 100000 Time , cost 33 second
testStringConcatenation02ByStringBuilder, String concatenation 100000 Time , cost 36 second
To class File directory , perform javap -c StringTest.class, Yes class Decompile files , see Compiled code differences . Don't use it here Intellij idea and JD decompile , Because decompilation has optimization , Will be decompiled into “+” Spliced , I can't see the real situation after compilation .
It can be seen that The two splicing methods are exactly the same after decompiling , There is no difference , The same goes for execution efficiency
Loop concatenates a string , Use StringBuilder
although “+” The concatenated string will also become StringBuilder, however Each cycle of processing will new One StringBuilder object , Time will be greatly increased . and Use it directly StringBuilder,new Just once , It's relatively efficient
/** * Recycling + String concatenation */
@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, String concatenation " + count + " Time , cost " + (endTime - startTime) + " second ");
}
/** * Test recycling StringBuilder Splicing strings takes time */
@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, String concatenation " + count + " Time , cost " + (endTime - startTime) + " second ");
}
testLoopStringConcatenation03ByPlus, String concatenation 10000 Time , cost 463 second
testLoopStringConcatenation04ByStringBuilder, String concatenation 10000 Time , cost 13 second
summary
Use... For circular splicing “+” String splicing is inefficient , Recommended StringBuilder String concatenation .
边栏推荐
- 134. 加油站
- C language to achieve three piece chess (not artificial mental retardation ha ha ha)
- 签到体系设计:签到功能该怎么画
- 【IoT】项目管理:如何打造更好的跨职能团队?
- [atcoder1998] stamp Rally
- 【AtCoder1983】BBQ Hard (组合数+巧妙模型转化)
- 2021-11-05 definition of cache
- Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
- 自定义ViewGroup的知识点总结-持续更新
- Implementation of stack (C language)
猜你喜欢

【AtCoder1980】Mysterious Light(数学模拟)

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...

C language - growth diary-04- preliminary exploration of local variables (local variables)

Paging of the flask page

如何做好空状态设计?来看这份全面总结

Data visualization and Matplotlib

C language Yanghui triangle code

3年功能测试拿8K,被新来的反超,其实你在假装努力

Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho

C# 微信上传Form-data
随机推荐
134. 加油站
[poj3691] DNA repair (AC automata +dp)
What exactly is PMP?
记一次忽略@SuppressLint(“NewApi“)提示引发的血案
How to output the percent sign "%" in printf function in C language
【AtCoder1998】Stamp Rally(整体二分+并查集)
排序——交换排序
零基础自学SQL课程 | OUTER JOIN外连接
2022.6.7 特长生模拟
Getting started with bladed tutorial (video)
[atcoder1981] short diameter (graph theory thinking)
学习《缠解论语》
空间几何
【IoT】项目管理:如何打造更好的跨职能团队?
About static keyword
Wc2020 course selection
Servlet、ServletConfig、ServletContext
Implementation of queue (C language)
运筹学导论
860. 柠檬水找零