Java Performance Tuning and Java Optimization Tips
Hi, I have taken this content from "http://www.glenmccl.com/jperf/index.htm" site.
This blog illustrates practical ways you can improve JavaTM performance tuning and Java efficiency techniques. These techniques focus on Java language and library features. Performance is defined to include both speed and space issues, that is, how to make your programs run faster, while using less memory and disk space. Many of the techniques are illustrated by code examples.
This blog raises a variety of performance issues, and gives some hard numbers about how specific performance improvements work out. The ultimate aim of the paper is to promote awareness of Java performance issues, so that you can make appropriate design and implementation choices for specific applications.
Table of Contents
Introduction
When to Worry About PerformancePerformance Issues Not Covered in This Paper
Just-in-Time Compilers and Java Virtual Machines
Environment and Tools Used in Code Examples
How Examples Were Timed
Performance Analysis Tools
Web Site
Classes
Default ConstructorsConstructor Hierarchies
Class and Instance Initialization
Recycling Class Instances
Methods
InliningFinal Methods
Synchronized Methods
Inner Classes
Strings
Strings are ImmutableAccumulating Strings Using char[] Arrays
Using == and String.equals() to Compare Strings
Interning Strings
Obtaining the Length of a String
Using toCharArray()
Converting Strings to Numbers
Input and Output
BufferingBufferedReader
Formatting
Obtaining File Information
Libraries
System.arraycopy()Vector vs. ArrayList
Setting Initial Array Capacity
ArrayList vs. LinkedList
Programming in Terms of Interfaces
Size
.class File SizeWrappers
Garbage Collection
SoftReference
BitSet
Sparse Arrays
1. Class and Instance Initialization
// class initialization public class cls_init1 { static class Data { private int month; private String name; Data(int i, String s) { month = i; name = s; } } Data months[] = { new Data(1, "January"), new Data(2, "February"), new Data(3, "March"), new Data(4, "April"), new Data(5, "May"), new Data(6, "June") }; public static void main(String args[]) { final int N = 250000; cls_init1 x; Timer t = new Timer(); for (int i = 1; i <= N; i++) x = new cls_init1(); t.print("data declared non-static"); } } // Timer class public class Timer { long t; // constructor public Timer() { reset(); } // reset timer public void reset() { t = System.currentTimeMillis(); } // return elapsed time public long elapsed() { return System.currentTimeMillis() - t; } // print explanatory string and elapsed time public void print(String s) { System.out.println(s + ": " + elapsed()); } }
// class initialization public class cls_init2 { static class Data { private int month; private String name; Data(int i, String s) { month = i; name = s; } } static Data months[] = { new Data(1, "January"), new Data(2, "February"), new Data(3, "March"), new Data(4, "April"), new Data(5, "May"), new Data(6, "June") }; public static void main(String args[]) { final int N = 250000; cls_init2 x; Timer t = new Timer(); for (int i = 1; i <= N; i++) x = new cls_init2(); t.print("data declared static"); } } // Timer class public class Timer { long t; // constructor public Timer() { reset(); } // reset timer public void reset() { t = System.currentTimeMillis(); } // return elapsed time public long elapsed() { return System.currentTimeMillis() - t; } // print explanatory string and elapsed time public void print(String s) { System.out.println(s + ": " + elapsed()); } }
This program requires 421 units of time to run, a saving of 10-1 over the first approach. Moreover, it saves a lot of space per class instance as well. As a general rule, it's worth "factoring out" methods that do not operate on unique data in a class instance, and variables that are not unique to an instance, and making both methods and variables static, that is, shared across all instances.
2. Inner Classes
// methods and inner classes public class meth_inner { private void f() {} class A { A() { f(); } } public meth_inner() { A a = new A(); } public static void main(String args[]) { meth_inner x = new meth_inner(); } }
A
is an inner class defined within meth_inner
, and A
's constructor calls a private method f()
defined in meth_inner
. Because the Java Virtual Machine has restrictions on calling private members from outside of their class, a special access method is generated by the compiler and added internally to the meth_inner
class. This method has a name access$0()
, and it in turns calls f()
. In other words, a method is generated that can be called from outside of meth_inner
, and grant access to a private method of meth_inner
. So when f()
is called above, a generated method access$0()
is called, and it in turn calls f()
.
If you use the JDK utility program that disassembles .class
files, by saying:
$ javap -c meth_inner
the output includes the following method definition:
Method void access$0(meth_inner) 0 aload_0 1 invokespecial #74 return
This is the body of the generated method.
You can avoid this overhead by avoiding use of private members in a class, assuming the class has inner classes that use those members. Obviously, however, there may be other reasons why private members are more important than the overhead associated with these generated methods.
3. Converting Strings to Numbers
// converting strings to numbers public class str_double { public static void main(String args[]) { final int N = 100000; Double d; // construct Double from string Timer t = new Timer(); for (int i = 1; i <= N; i++) d = new Double("12.34"); t.print("as string"); // construct Double from number t.reset(); for (int i = 1; i <= N; i++) d = new Double(12.34); t.print("as number"); } } // Timer class public class Timer { long t; // constructor public Timer() { reset(); } // reset timer public void reset() { t = System.currentTimeMillis(); } // return elapsed time public long elapsed() { return System.currentTimeMillis() - t; } // print explanatory string and elapsed time public void print(String s) { System.out.println(s + ": " + elapsed()); } }
Often you have no choice but to do conversion of strings to numbers, but it's worth keeping in mind the expense of this operation.
Contact the Author
Please send comments on this blog to "deepakmodi2006@gmail.com".
No comments:
Post a Comment