Java Performance Tuning-try/catch blocks and loops

Source: Internet
Author: User
Tags try catch

1 , JAVA Performance Tuning-Move the Try/catch block out of the loop

It is said that the Try/catch block into the circulation body, will greatly affect performance. The use of the Try/catch module makes the Java virtual machine do a lot of extra work. It's like saying to everyone, "Hey, man, there may be snakes on the road." So he heard the man had to hand with a stick, cautiously move forward.

Putting Try/catch blocks out of the loop is like saying to a large group of people, "Hey, guys, there may be snakes on the road." So the people who heard arranged for some people to go forward with a wooden stick, others basically unaffected. "

This theory is pretty good, the actual impact of the test on performance

2 , The source code of the Try/catch block in the loop condition

Package com.java.test;

Import java.util.ArrayList;

Import java.util.List;

/**

 * using the warm-up mode JVM parameters:-xx:printcompilation

 * Objective: To test the performance impact of using Try/catch in loops

*

* @author Fan Fangming

*/

public class Easytrycatchtest {

List<string>alist = new arraylist<string> ();

Public staticvoid Main (string[] args) throws Exception {

intwarmupcycles = 10000000;// warm -up times

inttestcycles = 50000000;// number of official executions

Easytrycatchtestse = new Easytrycatchtest ();

System.out.println (" ... the preheating cycle begins ... ");

Longcatchtime = Se.runcatchloop (warmupcycles);

Longnotcatchtime = Se.runcatchnotloop (warmupcycles);

System.out.println (" ... preheating End ");

System.out.println (" ... preheating phase, Try/catch in the cycle time: "+ catchtime);

System.out.println (" ... During the preheating phase, the Try/catch is not time consuming in the cycle: "+ notcatchtime);

System.out.println (" ... into the formal cycle ... ");

Catchtime =se.runcatchloop (testcycles);

Notcatchtime= Se.runcatchnotloop (testcycles);

System.out.println (" ... formal operation phase, Try/catch in the cycle time: "+ catchtime);

System.out.println (" ... In the formal operation phase, Try/catch is not time-consuming in the cycle: "+ notcatchtime);

}

Publiceasytrycatchtest () {

Alist.add ("0");

}

//Try/catch In the specific loop body (inner Ring Cycle)

Private Longruncatchloop (int iterations) {

        // Start Timing

Longstarttime = System.nanotime ();

for (intloop = 0; loop < iterations; loop++) {

try {

stringtemp = alist.get (0);

} catch (Exception e) {}

}

        // Timing Complete

Longelapsedtime = System.nanotime ();

return (Elapsedtime-starttime);

}

//Try/catch not in the specific loop body (inner Ring Cycle)

Public longruncatchnotloop (int iterations) {

        // Start Timing

Longstarttime = System.nanotime ();

try {

for (intloop = 0; loop < iterations; loop++) {

stringtemp = alist.get (0);

}

} catch (Exception e) {}

        // Timing Complete

Longelapsedtime = System.nanotime ();

return (Elapsedtime-starttime);

}

}

3 , running results

... Warm-up cycle begins ...

... Preheating End

... Preheating phase, Try/catch in cycle time: 76507316

... Preheating phase, Try/catch not in cycle time: 76292613

... Into the formal cycle ...

... Formal operation phase, try/catch in the cycle time: 389151690

... Formal operation phase, try/catch not in cycle time: 389874615

4 , conclusions

From the test results, it is possible that our JDK (1.6) will automatically optimize the bytecode, so whether the Try/catch in the loop, the overall performance impact is almost negligible, 389151690 389874615 is very small gap.

Therefore, in the actual work, you can consider the actual need to put the try catch block where needed.

Java Performance Tuning-try/catch blocks and loops

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.