Java code optimization

Source: Internet
Author: User

1. What is a good code?
Efficient and correct
easy to maintain
Simple and clear

2. How do I do it?
make the code cleaner than when you came.
make a meaningful name
don't repeat yourself .
note The code is neat

Remember: Don't think of yourself as a programmer, be an artist, the code from your hand is a work of art, try to make her elegant.

3. Principles
first, about annotations and documents
Good code does not require comments and documents (not including design documents).

we have to do, as far as possible to make your code easy to understand, if you do not, be sure to add easy-to-understand comment text. Don't be a tedious tirade.

Remember: All comments and documents in the code are floating clouds.

article two, about the code structure

don't keep piling code on demand

multi-object-oriented and interface-oriented programming to improve code extensibility and maintainability

Multi-use design patterns to improve code readability and flexibility to avoid duplication of your code

Remember: simple, elegant, clear code is the best way to reduce your workload and minimize problems.

article three, about testing
if possible, try to get all of your methods to have test code

if possible, apply test-driven development

if possible, try to run test cases before releasing the program and ensure that there are no problems

Remember: Code that is not tested is a cloud

4. Tips
1. Avoid duplication of initialization.

test1
HashMap map = new HashMap ();
    
Public Test1 () {
//Map = new HashMap ();
    }

Public static void Main (string[] args) {
int TT = 1000000;
Long T1 = System.currenttimemillis ();
for (int i=tt; i>=0;-I.) {
Test1 t = new Test1 ();
        }
System.out.println (System.currenttimemillis ()-t1);
        
    }

Repeat initialization time: 187 172
Normal initialization time: 94

test2
Public final static int max (int a, int b) {
return (a>b)? a:b;
    }

Public static void Main (string[] args) {
final int TT = 1000000;
int a = 5, b=10;
int C;
Long T1 = System.currenttimemillis ();
for (int i=tt; i>=0;-I.) {
C = Max (A, b);
        }
System.out.println (System.currenttimemillis ()-t1);
Long t2 = System.currenttimemillis ();
for (int i=tt; i>=0;-I.) {
C = (a>b)? a:b;
        }
System.out.println (System.currenttimemillis ()-t2);
        
    }

The final method compiles in an inline fashion, which improves performance, but looks like the new JVM is optimized to run slowly for the first time, with the same non-final and final performance.

test3
NewObject object=new NewObject ();
int value;
if (i>0) {
Value=object.getvalue ();
}
after optimization:
int value;
if (i>0) {
NewObject object = new NewObject ();
Value = Object.getvalue ();
}
nearest principle, try to use lazy loading strategy to reduce memory overhead

test4

list?alist=usvr.getuserinfolist ();
for (int i=0;i<alist.size (); i++) {
.....
}

after optimization:
for (int i=0,p=alist.size (); i<p;i++) {
.....
}
use local variables as much as possible to reduce memory addressing time when looping operations
About
0 0


Test5
Long T1 = System.currenttimemillis ();
for (int i = 0; i < 10000000; i++) {
Test1 t = new Test1 ();
    }
System.out.println (System.currenttimemillis ()-T1);
        
Long t2 = System.currenttimemillis ();
Test1 t = null;
for (int i = 0; i < 10000000; i++) {
t = new Test1 ();
    }
System.out.println (System.currenttimemillis ()-T2);

reducing the operation of the loop new variable, which saves n copies of the object's references in memory, wastes a lot of memory space and increases the GC
About
the

Test6
Avoid using method return values when making final conditional comparisons, which increases system overhead and reduces performance

While (IsTrue ()) {
...
}
after optimization:
boolean istrue= isTrue ();
While (isTrue) {
...
}

Test7
try to avoid the use of Try-catch blocks in the loop body, preferably in the try-cache outside the loop body to improve performance
do{
try{
        ...
} catch (Exception e) {}
}while (isTrue)
after optimization
try{
Do {
        ...
}while (isTrue)
}catch (Exception e) {}

Test8
In multiple loops, if possible, try to put the most of his cycle in the outermost layer, the shortest loop on the outer layers, to reduce the number of cycle switching.
for (int i=0; i<100000; i++) {
for (int j=0; j<10; J + +) {
        ...
    }
}
after optimization:
for (int j=0; j<10; J + +) {
for (int i=0; i<100000; i++) {
        ...
    }
}

Test9
There is if-else class logic inside the loop, it is best to move the if-else logic judgment out of the loop body to improve performance.
for (int i=0; i<100000; i++)
if (isTrue) {
Dothis
} else {
Dothat
    }
}
after optimization:
if (isTrue) {
for (int i=0; i<100000;i++) {
Dothis
    }
} else {
for (int i=0; i<100000;i++) {
Dothat
    }
}

Java code optimization

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.