Java 实例 - 字符串优化
Java 教程
· 2019-02-10 09:13:27
以下实例演示了通过 String.intern() 方法来优化字符串:
StringOptimization.java 文件
public class StringOptimization {
public static void main(String[] args){
String variables[] = new String[50000];
for( int i=0;i <50000;i++){
variables[i] = "s"+i;
}
long startTime0 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = "hello";
}
long endTime0 = System.currentTimeMillis();
System.out.println("直接使用字符串: "+ (endTime0 - startTime0) + " ms" );
long startTime1 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("使用 new 关键字:" + (endTime1 - startTime1) + " ms");
long startTime2 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
variables[i] = variables[i].intern();
}
long endTime2 = System.currentTimeMillis();
System.out.println("使用字符串对象的 intern() 方法: "
+ (endTime2 - startTime2)
+ " ms");
}
}
以上代码实例输出结果为:
直接使用字符串: 3 ms 使用 new 关键字:5 ms 使用字符串对象的 intern() 方法: 10 ms
点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html
Stylin' with CSS
Wyke-Smith, Charles / 2012-10 / $ 50.84
In this completely revised edition of his bestselling Stylin' with CSS, veteran designer and programmer Charles Wyke-Smith guides you through a comprehensive overview of designing Web pages with CSS, ......一起来看看 《Stylin' with CSS》 这本书的介绍吧!