3. latency expression CalculationIn Java, all the method parameters will be calculated first (left to right) as long as there is a method parameter that is an expression before the method is called ). This rule causes unnecessary operations. Consider the following scenario: Use ComparisonChain to compare two Foo objects. One advantage of using such a comparison chain is that in the comparison process, the entire comparison ends if a compareTo method returns a non-zero value, avoiding many unnecessary comparisons. For example, in this scenario, the objects to be compared first consider their score, then the position, and finally the _ bar attribute:
public class Foo {private float _score;private int _position;private Bar _bar; public int compareTo (Foo other) {return ComparisonChain.start().compare(_score, other.getScore()).compare(_position, other.getPosition()).compare(_bar.toString(), other.getBar().toString()).result;}}
However, the above implementation will always convert two String objects to save the bar. toString () and other. getBar (). the value of toString (), even if the two strings are not required for comparison. To avoid such overhead, You can implement a comparator for the Bar object:
public class Foo {private float _score;private int _position;private Bar _bar;private final BarComparator BAR_COMPARATOR = new BarComparator(); public int compareTo (Foo other) {return ComparisonChain.start().compare(_score, other.getScore()).compare(_position, other.getPosition()).compare(_bar, other.getBar(), BAR_COMPARATOR).result();}private static class BarComparator implements Comparator
{@Overridepublic int compare(Bar a, Bar b) {return a.toString().compareTo(b.toString());}}}
4. compile regular expressions in advanceString operations are costly operations in Java. Fortunately, Java provides some tools to make regular expressions as efficient as possible. Dynamic regular expressions are rare in practice. In the following example, each call to String. replaceAll () contains a constant mode applied to the input value. Therefore, we can pre-compile this mode to save CPU and memory overhead.
Before optimization:
private String transform(String term) {return outputTerm = term.replaceAll(_regex, _replacement);}
-
- After optimization:
private final Pattern _pattern = Pattern.compile(_regex);private String transform(String term) {return outputTerm = _pattern.matcher(term).replaceAll(_replacement);}
5. Cache it as much as possible if you canSaving results in the cache is also a way to avoid overhead. Multiple LRU (Least Recently Used) cache algorithms are available.
6. The intern method of String is useful but dangerous.The intern feature of String can sometimes be used instead of cache.
From this document, we can know:
"A pool of strings, initially empty, is maintained privately by the class String. when the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals (Object) method, then the string from the pool is returned. otherwise, this String object is added to the pool and a reference to this String object is returned ".
This feature is similar to caching, but there is a limit that you cannot set the maximum number of elements to accommodate. Therefore, if there is no limit on these intern strings (for example, strings represent some unique IDs), it will cause a rapid increase in memory usage.