Improve Code Performance Efficiency summary (b)--java

Source: Internet
Author: User

Continue with the summary of code optimization.

7. Cycle optimization

List alist =getlist ();

for (int i=0;i<alist.size (); i++) {}

The code will always execute Alist.size ()

should be amended to read

for (int i=0,p=alist.size (); i<p;i++) {}

8. Objects that "eliminate" the non-visual stage

try{

Object Obj=new object ();

}catch (Exception e)

{

Obj=null; Add this code here

}

9. Create objects less with new

When you use the keyword new to create an instance of a class, all constructors in the constructor chain are called automatically, but if an object implements the Cloneable interface, we can call its clone () method.

public static Credit Getnewcredit () {

return new credit ();

}

Modified to:

private static Credit Basecredit = new Credits ();

public static Credit Getnewcredit () {

return (Credit) Basecredit.clone ();-

}

When new creates an object, try to create the object when it is used

NewObject obj=new NewObject ();

if (i>0) {

Value=object.getvalue ();

}

Modified to:

if (i>0) {

NewObject obj=new NewObject ();

Value=object.getvalue ();

}

10. Release unused objects in a timely manner

For example, when a is not empty,

A=new Object ();

Modified to:

A=null;

A=new Object ();

11. Use your own method

When working with strings, use as much of the methods as possible with objects such as String.IndexOf (), String.LastIndexOf (), and so on, than do the same thing in a Java loop 10~100 times.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Improve Code Performance Efficiency summary (b)--java

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.