9 java performance tuning method of finishing

1, use StringBuilder

StingBuilder should be the default in our Java code to use, you should avoid using the + operator.

In general, use of more effective than using StringBuilder + operator. If possible, please Methods in need across multiple StringBuilder case of passing references, because String consumes additional resources.

If you are still using StringBuffer, then use  StringBuilder  instead of StringBuffer, after all, the situation needs to be synchronized string of really small.

2, avoid the use of regular expressions

If the last resort have to use regular expressions in compute-intensive code, then at least to the Pattern cached to avoid repeated compilation Pattern.

Regular expressions are very useful, but also in the use of a price to pay. Also be careful to use a variety of JDK regular expression string method, such as String.replaceAll () or String.split (). You can choose to use the more popular development libraries, such as Apache Commons Lang string to operate.


3. Do not use the iterator () method

Whether it is easy to read and write from the perspective of, that was designed from the perspective of API for iterators, Iterable interface, and is very easy to use a foreach loop. But the tradeoff is that when they are using the heap for each extra child circulation to create an object. If the loop to be performed many, many times, please avoid meaningless generated instance, preferably with substantially circular manner in the place of the pointer to the iterator, and the Iterable foreach loop interfaces. Reference http://www.wityx.com/post/286_1_1.html

4, do not call the high cost method

Some great cost method, replacement method call is cached in the leaf nodes of the high cost method, or to avoid high overhead method call in the case of the agreed allowed.

5, the original type and stack

Avoid using wrapper classes. Bad packaging is to bring a lot of pressure GC, GC will be to remove the object wrapper class generated and busy.

Therefore, an efficient optimization method is to use the basic data types, fixed-length array, and a series of variable to identify the position of the divided object within the array.

Other basic types of integers have a similar situation, such as char, short, int, long.

These basic types of integer not automatically packing or calls TheType.valueOf () method is a constructor is invoked.

Do not call the constructor in the wrapper class, unless you do not want to create an instance of the heap. The benefit of this is to offer colleagues a giant pit of your April Fool's Day joke.

Of course, if you still want to experience heap outside the library, then, although this may mingled with a lot of strategic decisions, rather than the most optimistic of the local program.

6, avoid recursion

Now, this is similar to Scala functional programming languages ​​encourage the use of recursion. Because usually it means recursively decompose to individual ones of the optimized tail recursion (tail-recursing). If you use a programming language to support it is very good. But even so, we must pay attention to subtle adjustments to the tail-recursive algorithm will become ordinary recursion.

Want the compiler can automatically detect this, otherwise we would have been just use local variables able to get a few things and wasted a lot of stack frame (stack frames). Reference http://www.wityx.com/post/286_1_1.html

So try to use iteration instead of recursion.

7, using entrySet ()

When we want to traverse with a key-value pair in the form of saved Map, if you have to use the map, then use entrySet () method to iterative! In this case, we want to access it is merely an example of Map.Entry.

8, or use EnumSet EnumMap

In some cases, such as when using the configuration map, we may know in advance stored in the key map. If this key is very small, we should consider using EnumSet or EnumMap, rather than our usual HashSet or HashMap.

9, custom optimization hasCode () method and equals () method

In the case of not using EnumMap, at least optimization hashCode () and equals () methods. A good hashCode () method is necessary because it prevents unnecessary calls to high-cost equals () method. In each class inheritance structure, simple object needs to be easily accepted.

Guess you like

Origin www.cnblogs.com/javamianshiti/p/11573871.html