JAVA Program Performance Tuning notes (3)-tips to help improve performance __java

Source: Internet
Author: User
Tags bitwise instance method int size

Title:java Program Performance Tuning notes (3)-tips to help improve performance
Tags: tips to help improve performance grammar_cjkruby:true 1. Use exceptions with caution

    @Test public
    void Testinfortrycatch () {//performance difference
        long Starttime=system.currenttimemillis ();
        int a=0;
        for (int i=0;i<100000000;i++) {
            try{
                a++;
            } catch (Exception e) {
            }
        }
        long Endtime=system.currenttimemillis ();
        System.out.println ("Testinfortrycatch:" + (Endtime-starttime));
    }


    @Test public
    void Testoutfortrycatch () {//performance good
        long Starttime=system.currenttimemillis ();
        int a=0;
        try{for
            (int i=0;i<100000000;i++) {
                a++
            }
        } catch (Exception e) {
        }
        long Endtime=system.currenttimemillis ();
        System.out.println ("Testoutfortrycatch:" + (Endtime-starttime));
    }
2. Using Local variables

Local variables are accessed much faster than member variables of the class

    @Test public
    void Testlocalvar () {
        long starttime=system.currenttimemillis ();
        int a=0;
        for (int i=0;i<100000000;i++) {
            a++
        }
        Long Endtime=system.currenttimemillis ();
        System.out.println ("Testlocalvar:" + (Endtime-starttime));
    }

    @Test
    privat static int ta = 0;
    public void Testmembervar () {
        long starttime=system.currenttimemillis ();
        for (int i=0;i<100000000;i++) {
            ta++
        }
        Long Endtime=system.currenttimemillis ();
        System.out.println ("Testmembervar:" + (Endtime-starttime));
    
3. The position operation replaces the multiplication and division method

There are not many people who can get this.
Common operations and topics for bitwise operations
A rule of classical bitwise operation 4. Replace switch (jdk1.7 above version switch performance to correct) 5. One-dimensional array replaces two-dimensional arrays

If the array.length is proposed, especially for two-dimensional arrays, it will have a good optimization effect. 6. Extract an expression

 @Test public void Testweitiqu () {double d=math.random ();
        Double A=math.random ();
        Double B=math.random ();
        Double E=math.random ();
        Double x,y;
        Long Starttime=system.currenttimemillis ();
            for (int i=0;i<10000000;i++) {x=d*a*b/3*4*a;
        y=e*a*b/3*4*a;//repetition should be abstract} long Endtime=system.currenttimemillis ();
    System.out.println ("Testweitiqu:" + (Endtime-starttime));
        @Test public void Testtiqu () {double d=math.random ();
        Double A=math.random ();
        Double B=math.random ();
        Double E=math.random ();
        Double t,x,y;
        Long Starttime=system.currenttimemillis ();
             for (int i=0;i<10000000;i++) {t=a*b/3*4*a; X=d*t;
        After the abstraction, the speed is elevated y=e*t;
        Long Endtime=system.currenttimemillis ();
    System.out.println ("Testtiqu:" + (Endtime-starttime)); }
7. Expand the Cycle

By definition, the method reduces the number of cycles and effectively reduces the time. 8. Boolean operations in place of bitwise operations

We seldom use bit arithmetic, so we can understand it. 9. Use of Arraycopy ()

    @Test public
    void Testarraycopysystem () {
        int size=100000;
        Int[] Array=new int[size];
        Int[] Arraydst=new int[size];
        for (int i=0;i<array.length;i++) {
            array[i]=i
        }
        Long Starttime=system.currenttimemillis ();
        for (int k=0;k<1000;k++)
            system.arraycopy (array, 0, ARRAYDST, 0, size);
        Long Endtime=system.currenttimemillis ();
        System.out.println ("Testarraycopysystem:" + (Endtime-starttime) + "Last:" +arraydst[size-1]);
    }
10. Use Clone () instead of new

Used on large objects, small objects with new foot can be. Can refer to Java cloning Technology 11. Static method Instead of instance method

The static method is obviously fast and the instance method.

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.