当前位置:网站首页>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 .
边栏推荐
- C language to achieve three piece chess (not artificial mental retardation ha ha ha)
- Rabin Miller prime test
- [atcoder2305] declining (game)
- Methods to improve training speed in deep learning and techniques to reduce video memory (suitable for entry-level trick without too many computing resources)
- C- print 99 multiplication table
- Understanding of Poisson distribution and Poisson process and Erlang distribution and their relations (important theories in queuing theory and operational research)
- 记一次忽略@SuppressLint(“NewApi“)提示引发的血案
- 20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
- TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
- VIM common commands
猜你喜欢

签到体系设计:签到功能该怎么画

空间几何

Use of wordcloud

Import on CSDN MD file

Zero foundation self-study SQL course | union joint query

Zero foundation self-study SQL course | outer join external connection

TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者

Bladed入门教程(视频)

Paging of the flask page
![[software testing] 90% of the interviewers have been brushed out of such resumes](/img/2f/bb4819b98592f750dec92d4b4dd6b7.png)
[software testing] 90% of the interviewers have been brushed out of such resumes
随机推荐
Introduction to operations research
[untitled] Weng_ C lesson 1
Sort - merge sort
pmp到底是什么?
Session and session management technology
[codeforces1019e] raining season
You got 8K in the 3-year function test, but you were actually pretending to work hard
【集群】haproxy负载均衡
排序——归并排序
Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases
20200810 T2 dispatch money
Methods to improve training speed in deep learning and techniques to reduce video memory (suitable for entry-level trick without too many computing resources)
[noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
Paging of the flask page
20200803 T3 my friends [divide and conquer NTT optimization recursion]
Storage of floating point in memory
代码设置ConstraintLayout的layout_constraintDimensionRatio
C language - growth diary-04- preliminary exploration of local variables (local variables)
Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np