[] Performance Optimization efficient code articles (1)

A string

1 using the String.intern () to save memory
 2 . Long string concatenation, using the StringBuilder shown to improve the performance of a string of splicing

 

Second, careful use of regular expressions, such as the String.split ( "specifies a regular expression") method

In programming languages, regular expression library use are based on NFA automatic machine implementation, there is a problem backtracking NFA automatic machine
I can not to do
How to avoid backtracking, using the lazy, and the exclusive mode
1 . Greedy " ab & C {l, 3} "
Regex will match as much content, backtracking
2 . Lazy mode " ab & l, 3} {? C "
As few duplicate matching character
3 . Exclusive " ab & l, 3} + {BC "
Regex will match as much content, not back, will end the match fails
optimization:
1 . Less greedy, the use of exclusive
 2 . Reducing branch selection
Such as: " (X | Y | Z) " , try not to use
Second, the common mode can be extracted, " (ABCD | ABEF) " -> " ab & (CD | EF) " 
. 3 . Nesting reduce trapped
(): Capture group
( ? : Exp): not capture
如:"(<input.*?>)(.*?)(</input>)" -> "(?:<input.*?>)(.*?)(?:</input>)"

 

Three, ArrayList and LinkedList

1 . When creating ArrayList object size specified initial capacity as far as possible, to prevent performance degradation resulting in expansion of
 2 .ArrayList in case of expansion does not occur, for a set of intermediate and aft positions when adding and deleting operations, higher performance ArrayList of LikedList
 3 . traverse the set scene, for (;;) and iterator () in two ways,
 for (;;) mode: ArrayList performance than the LinkedList (because ArrayList support random access;)
iterator () mode: ArrayList LinkedList performance properties approximately equal to
So, LinkedList use the iterator iterator to traverse the elements, ArrayList for use through the elements

Guess you like

Origin www.cnblogs.com/756623607-zhang/p/11141303.html