(translation www.java-performance.com) improve Java program Performance--JDK (v)

Source: Internet
Author: User
Tags crc32

1. Never call the valueof (String) function of a java.lang.Number subclass. If you need a value of the original type use the Parse[type] function. If you do need a wrapper class of the original type, or call the Parse[type] function, then rely on the JVM to automatically wrap it. Because the JVM supports caching most commonly used values. Never call the wrapper class's constructor, because it always returns a new object, which will bypass the JVM's caching capabilities. The following table is an original type of cache scope:
Byte, short, long:from-128 to 127
Character:from 0 to 127
integer:from-128 to Java.lang.Integer.IntegerCache.high or 127, whichever is bigger
Float, Double: Do not cache
Example with integer: Try to use Integer.parseint () instead of integer.valueof ()
--------------------------------------------------------------------------------
Here are some optimizations for the existence of elements in a collection (Collection):
1. For set, there is no need to determine and add or delete (contains () + Add ()/remove ()), directly using the Add (+)/remove ()).
2. For map, use the map's get () directly before the value, and then judge by whether the return value is null, rather than using the CONTAINS function first, then get. When you delete a value, you call the Remove function directly, and then you determine the result. Instead of calling the CONTAINS function first, perform the remove operation.
Summary: Some operations of the collection can be performed first, then judged by the results, rather than using the CONTAINS () function to judge the execution, so that a lookup operation is missing.
--------------------------------------------------------------------------------
Java.util.zip.CRC32, Java.util.zip.Adler32 and java.util.zip.Checksum: not translated, see original.
--------------------------------------------------------------------------------
1. When implementing the Hashcode function of a class, it is much more important to improve the uniform distribution of hash values than the speed of cloud-optimized hashcode functions. Never let the hashcode function return a constant.
2. The hash value distribution generated by the Hashcode function of the string class is almost perfect, so in some cases you can use a string hash value instead of a string. If you is working with sets of strings, try-to-end up with BitSets, as described on this article.

Original:

Java.lang.Byte, short, Integer, Long, Character (boxing and unboxing):,,,, java.lang.Byte java.lang.Short java.lang.Integer java.lang.Long java.lang.Character :

Tags: Low latency, high throughput, CPU optimization, memory optimization.

    • Never call java.lang.Number subclasses valueOf(String) methods. If you need a primitive value-call parse[Type] . If you want an instance of a wrapper class, still call parse[Type] method and rely on the jvm-implemented boxing. It would support caching of the most frequently used values. Never call wrapper classes Constructors-they always return a new Object , thus bypassing the caching support. Here are the summary of caching support for primitive replacement classes:
Byte, Short, Long Character Integer Float, Double
From-128 to 127 From 0 to 127 From-128 to Java.lang.Integer.IntegerCache.high or 127, whichever is bigger No Caching

Map.containskey/set.contains: java.util.Map , and most of java.util.Set their implementations:

Tags: Low latency, high throughput, CPU optimization, Java collections.

    • For sets, the call pairs should is replaced with a single contains+add/remove add/remove calls even if some extra logic is guarded by call contains .
    • For maps, contains+get pair shall-replaced with get followed by null -check of get result. contains+remove pair should b E replaced with a, and check of its remove result.
    • Same ideas is applicable to Trove maps and sets too.

Java.util.zip.CRC32 and Java.util.zip.Adler32 performance: java.util.zip.CRC32 , java.util.zip.Adler32 and java.util.zip.Checksum :

Tags: CPU optimization, checksum.

    • If you can choose which checksum implementation you can use-try first Adler32 . If its quality are sufficient for your, use it instead of CRC32 . In any case, use Checksum interface in order to access Adler32/CRC32 logic.
    • Try to update checksum by at least-blocks. Shorter blocks would require a noticeable time to being spent in JNI calls.

Hashcode Method Performance Tuning: java.lang.String ,,, java.util.HashMap java.util.HashSet java.util.Arrays :

Tags: Low latency, high throughput, CPU optimization, memory optimization.

    • Try to improve distribution of results of your hashCode method. This was far more important than to optimize. Never write a hashCode method which returns a constant.
    • String.hashCodeResults distribution is nearly perfect, so can sometimes substitute String s with their hash codes. If you is working with sets of strings, try-to-end up BitSet with S, as described in this article. Performance of your code would greatly improve.

(translation www.java-performance.com) improve Java program Performance--JDK (v)

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.