Share several efficient uses for improving Java performance _java

Source: Internet
Author: User
Tags stringbuffer

1. In the important cycle, the elimination of cyclic termination judgment when the method call

Copy Code code as follows:

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

To replace with ...

Copy Code code as follows:

View Plaincopy to Clipboardprint?
for (int i=0;n=collection.size (); i<n;i++)
{
...
}

2. Usually, move the loop index irrelevant to the outside of the loop

Copy Code code as follows:

for (int i=0;terminal=x.length;i<terminal;i++) {
X[i]=x[i]/scalea*scaleb;
}
for (int i=0;terminal=x.length;i<terminal;i++) {
X[i]=x[i]/scalea*scaleb;
}

To

Copy Code code as follows:

Double scale = Scaleb/scalea;
for (int i=0; terminal=x.length; i<terminal; i++) {
X[i]=x[i]*scale;
}

2. String

Eliminate string concatenation
Always use Stringbuffter instead of string when creating long strings
Pre-allocating StringBuffer space

StringBuffer sb = new StringBuffer (5000);

3. Basic data types

Use basic data types in important loops (int-type data is usually faster than long/double data)
The wrapper class for the base data type (BOOLEAN,INTEGER,ETC) is primarily used when the method parameter passed must be a reference to an object (not a basic data type)
Use the static final modifier for all constant algebraic expressions

One makes constants easier to reference (the compiler computes constant expressions in advance)

4. Abnormal

Exceptions are only used for a single true error condition

It is expensive to throw an exception and execute a catch code block (mainly because you want to get a snapshot of the line stacks when an exception is created)
An exception is thrown when a condition is really an exception

The compiler and runtime are optimized to put several method calls in a Try/catch block instead of implementing several Try/catch blocks for each method call

5. Benchmark

Note that all of these techniques will vary depending on the platform and the virtual machine

One example: In some servlet containers, it is faster to pass a outputstream as a byte output
One in the other container, the output character through a printwriter will be faster

These techniques describe the most portable recommendations.

You may need to run some benchmarks to determine what's the fastest on your platform.

Related Article

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.