package com.jdk.api.test;
public class Strings {
public static void main(String[] args)
{
String a1 = null;
// 事实证明证明使用stringbuilder 比使用string 连加操作快很多
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
a1=a1+"al";
}
long end = System.currentTimeMillis();
System.out.println("--:"+(end-start));
start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
sb = sb.append("sb");
}
end = System.currentTimeMillis();
System.out.println("--:"+(end-start));
}
/*
* --:39394
--:13
*/
}
public class Strings {
public static void main(String[] args)
{
String a1 = null;
// 事实证明证明使用stringbuilder 比使用string 连加操作快很多
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
a1=a1+"al";
}
long end = System.currentTimeMillis();
System.out.println("--:"+(end-start));
start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
sb = sb.append("sb");
}
end = System.currentTimeMillis();
System.out.println("--:"+(end-start));
}
/*
* --:39394
--:13
*/
}
本文通过编程实验比较了使用StringBuilder与直接字符串连接在Java中的性能差异。结果显示,在大量字符串拼接操作中,StringBuilder表现出显著的性能优势。
489

被折叠的 条评论
为什么被折叠?



