String variables[] = new String[50000];
for( int i=0;i<variables.length;i++){
variables[i] = "s"+i;
}
// create String literals
long startTime0 = System.currentTimeMillis();
for(int i=0;i<variables.length;i++){
variables[i] = "hello";
}
long endTime0 = System.currentTimeMillis();
System.out.println("Time taken for creation of String literals : "
+ (endTime0 - startTime0) + " milli seconds" );
// create String objects using 'new' keyword
long startTime1 = System.currentTimeMillis();
for(int i=0;i<variables.length;i++){
variables[i] = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("Time taken for creation of String objects with 'new' key word : "
+ (endTime1 - startTime1)+" milli seconds");
long startTime2 = System.currentTimeMillis();
for(int i=0;i<variables.length;i++){
variables[i] = new String("hello");
variables[i] = variables[i].intern();
}
long endTime2 = System.currentTimeMillis();
System.out.println("Time taken for creation of String objects with intern(): "
+ (endTime2 - startTime2)+" milli seconds");
这段代码在文章中给出的结果为:
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects with 'new' key word : 160 milli seconds
Time taken for creation of String objects with intern(): 60 milli seconds
但是我在电脑上实际运行结果为:
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects with 'new' key word : 3 milli seconds
Time taken for creation of String objects with intern(): 7 milli seconds
刨除硬件运行速度差异,明显区别还是String.intern()执行到底有没有带来效率上的提高.似乎我的运行结果与作者要说明的事实恰恰相反.
分享到:
相关推荐
Java 9 High Performance 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
When O’Reilly first approached me about writing a book on Java performance tuning, I was unsure. Java performance, I thought—aren’t we done with that? Yes, I still work on performance of Java (and ...
Java 9 High Performance 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或...
Java(TM) Performance and Scalability, Volume 1, provides invaluable advice that you will, no doubt, find useful in your coding. Presented in 48 concise lessons that target the most common and ...
本书《Java Performance Tuning, 2nd Edition》由Jack Shirazi编写,旨在为Java开发者提供全面且深入的性能调优指南。无论是对初学者还是经验丰富的开发人员来说,书中涵盖的知识点都是极其宝贵的资源。 #### 二、...
Java® Performance Companion shows how to systematically and proactively improve Java performance with today’s advanced multicore hardware and complex operating system environments. The authors, who...
《Java Performance》是一本深入探讨Java性能优化的经典著作,作者Charlie Hunt是Java性能领域的权威专家。这本书详尽地阐述了如何理解和改善Java应用程序的性能,包括JVM(Java虚拟机)的工作原理、性能分析工具的...
Title: Java EE 7 Performance Tuning and Optimization Author: Osama Oransa Length: 398 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-06-25 ISBN-10: 178217642X ...
标题“trouble shooting java performance”直接指出了本书的主题——解决Java性能问题的故障排除方法。作者Erik Ostermueller通过使用开源工具来检测Java应用程序中的反模式,提出了一种可重复的方法来处理基于Java...
《Java Performance》是一本专注于Java程序性能优化的专业书籍,它为开发者提供了深入理解并提升Java应用程序性能的关键知识。这本书详细探讨了如何在Java开发中识别和解决性能问题,旨在帮助开发者构建更快、更高效...
High Performance Java Persistence 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系...
根据给定的信息,我们可以深入探讨Java性能优化的关键领域,尤其是JDK版本的选择、字符串操作以及对象池技术等几个方面。 ### JDK版本选择的重要性 在Java开发过程中,选择合适的JDK版本至关重要。不同的JDK版本...
Java: High-Performance Apps with Java 9: Boost your application’s performance with the new features of Java 9 By 作者: Mayur Ramgir – Nick Samoylov ISBN-10 书号: 1789130514 ISBN-13 书号: ...
Gain in-depth knowledge of Java application performance, using the Java Virtual Vachine (JVM) and the Java platform, including the language and API. With this comprehensive guide, developers and ...
Java Performance Companion 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或...