Why is Java slower than C ++? And how to improve Java Efficiency

Source: Internet
Author: User

Why is Java slow?

1. Java Memory Allocation
Java only allocates embedded types to the stack, and all object types to the heap.
C ++ can allocate local variables to the stack.
Obviously, according to the existing test results, the speed of accessing the stack is much higher than that of the heap.

Revelation 1: C ++ does not return structure. You can use references to first input the returned object;
Revelation 2: Do not discard Java objects and extend their lifecycles as much as possible; but it does not mean to maintain an object pool;

2. Java has no template
The disadvantage of no template is that type conversion is required. Type conversion is a very time-consuming operation.

How to Reduce type conversion?

3,

4. What is the memory usage of C ++?
The hashmap (INT, INT) provided by tr1 occupies 16 bytes, which is twice the actual data;
The map (INT, INT) that comes with STL occupies 32 bytes, 4 times the actual data;
If you write your own data, it is usually about 1.2 times the occupied space.

5. Why is the underlying structure of the Java version inefficient?
A) Java does not support array-to-object conversion.
B) the memory location of the Java object is automatically allocated and cannot be controlled.

 

Memory usage of the Java Basic Structure

A) each object occupies 8 bytes.
B) object 8-byte alignment
C) array occupies 12 bytes.

Java underlying optimization suggestions
Written by goodzhu

1. Rule No1: Do not optimize the code!
Note: you must write readable code;

2. Rule NO2: select the optimization target
Note: Generally, only about 20% of the Code is the bottleneck.

3. General Optimization Technology

A. Simple Operators
Note: For example, to simplify "/= 2 ^ N" to "> = N ";

B. Common expression Extraction
Note:
Double X = D * a * B;
Double Y = E * a * B;
It can be changed:
C = A * B;
X = D * C;
Y = E * C; // the last two sentences can be computed in parallel at the CPU level.

C. Pre-calculated unchanged values
For example:
For (INT I = 0; I <n; I ++)
X [I] = math. Pi * Math. Cos (y) * I;
It can be changed:
Double D = math. Pi * Math. Cos (y );
For (INT I = 0; I <n; I ++)
X [I] = D * I;

D. Partially expand and loop
For example:
For (INT I = 0; I <n; I ++)
X [I] = I;
It can be changed:
For (INT I = 0; I <n; I + = 3)
{
X [I] = I;
X [I + 1] = I + 1;
X [I + 2] = I + 2;
}

4. Try end Loop Technology
For example:
For (INT I = 0; I <A. length; I ++)
A [I] = I * I;
To:
Try {
For (INT I = 0; I ++)
A [I] = I * I;
} Catch (exception nouse)
{
}
Note: This technology saves one judgment and is effective only when the number of cycles is large (over 10 million;

5. Description of the Basic Data Structure

A. Copy the Array Using system. arraycopy
B. When the arrays. sort method is used in the SORT object array, it adopts stable Merge Sorting.
C. The arrays. sort method does not use introsort when embedding arrays of the sort type. Do not expect it to have the speed of STL: sort.
D. The collections. sort method first copies the list into an array and then calls arrays. sort, which is very slow.
E. The proportion of auto-growth in arraylist is 1.5, that is, the auto-growth rate of 50% (+ 1) is not enough)
F. bitset is implemented using the long array internally. The speed on 32-bit machines is not high.
The default filling rate of G and hashmap is 0.75.
H. priorityqueue is implemented by the minimum heap value, but it is slow. When the number is large, it is inferior to treemap.
I, vector, and arraylist cannot be used as Arrays for deletion operations. If necessary, we recommend that you implement it by yourself. You can adopt a delayed deletion policy.
J. Map and hashmap are the same as STL: map and STL: hashtable of C ++.
K. The difference between stringbuilder and stringbuffer is that the latter can be called in multiple threads (the speed is slower)
L. Pay attention to the famous string Memory leakage problem! Do not call the substring method unless the original string is long. Copy it directly.

6. Regular Expression description

A. Pay attention to the string. split method. The efficiency is very low. The expression needs to be compiled every time!
B. Do not use:
X +
To:
X {1,1024}
The reason is that when the input is 1000 X, the stack (that is, function call) has 1000 layers, and the memory is insufficient;
C,

7. Select the latest JVM and adopt-server options
Note: The JVM will be pre-compiled only when the-server option is used (otherwise, it depends on the mood of hotspot)

Misunderstanding:

1. The method must be added with final, and no non-final local variables can be used for inline
Note: All methods can be inline (although the JVM may choose not inline)

2. The class must be added with final to enable the method to be inline
Note: the benefit of hotspot is that it has the ability to deoptimize. When detecting that the method should not be inline, it can correct itself and re-inline it.
Note: of course, if final can be added, hotspot can be used to avoid additional work.

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.