Java program performance optimization

Source: Internet
Author: User

Performance Optimization of Java programs
Let's take a look at another common Java class ?? Java. util. vector. Simply put, a vector is an array of Java. Lang. object instances. A vector is similar to an array. Its elements can be accessed through an integer index. However, after a vector object is created, the object size can be expanded or reduced based on the addition or deletion of elements. Consider the following example of adding elements to a vector:

Object OBJ = new object ();
Vector v = new vector (100000 );
For (INT I = 0;
I <100000; I ++) {v. Add (0, OBJ );}
Unless there are absolutely good reasons to require that the new elements be inserted before the vector each time, the above Code is not good for performance. In the default constructor, the initial storage capacity of the vector is 10 elements. If the storage capacity of the new element is insufficient when it is added, the storage capacity will be doubled each time. Like the stringbuffer class, the vector class copies all existing elements to the new storage space each time it expands the storage capacity. The following code snippets are several orders of magnitude faster than the previous example:

Object OBJ = new object ();
Vector v = new vector (100000 );
For (INT I = 0; I <100000; I ++) {v. Add (OBJ );}
The same rule applies to the remove () method of the Vector class. Because each element in a vector cannot contain a "gap", deleting any element other than the last element causes the element to move forward after it is deleted. That is to say, deleting the last element from a vector is several times less overhead than deleting the first element.

We can use this code to delete all elements from the previous vector:

For (INT I = 0; I <100000; I ++)
{
V. Remove (0 );
}
However, compared with the following code, the previous code is several orders of magnitude slower:

For (INT I = 0; I <100000; I ++)
{
V. Remove (V. Size ()-1 );
}
The best way to delete all elements from Vector object V is:

V. removeallelements ();
Assume that the vector object V contains the string "hello ". Consider the following code. It will delete the "hello" string from this vector:

String S = "hello ";
Int I = V. indexof (s );
If (I! =-1) v. Remove (s );
These codes seem to have no errors, but they are also detrimental to performance. In this Code, the indexof () method searches for the string "hello" in sequence, and the remove (s) method also performs the same sequential search. The improved version is:

String S = "hello ";
Int I = V. indexof (s );
If (I! =-1) v. Remove (I );
In this version, the exact index position of the element to be deleted is directly given in the remove () method, thus avoiding the second search. A better version is:

String S = "hello"; V. Remove (s );
Finally, let's look at a code snippet about the Vector class:

For (INT I = 0; I ++; I
If V contains 100,000 elements, the code snippet calls the v. Size () method 100,000 times. Although the size method is a simple method, it still requires the overhead of a method call. At least JVM needs to configure and clear the stack environment for it. Here, the code inside the for loop does not modify the V Size of the vector object in any way, so the above Code should be rewritten to the following form:

Int size = V. Size (); For (INT I = 0; I ++; I
Although this is a simple change, it still wins the performance. After all, every CPU cycle is precious.
This post comes from the group music community-http://q.yesky.com/group/review-8344349.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.