Write by yourself recentlyProgramTo improve the performance of the program.
A basic idea is to reduce the creation of objects. Because our program requires a lot of integer operations, including integer. tostring (), integer. tohexstring () and so on. In theory, each time you call these functions, an integer is parsed and a string is generated. Therefore, I write a class to cache the results. I don't have to calculate the result for the second call.
BelowCodePublished in the public domain, you can use them freely.
/** <Br/> * integer utils, cached batch results of tostring/tohexstring to get better performance <p> <br/> * Java 1.4 compatible <br/> * @ author henix [http://blog.csdn.net/shell_picker/> */<br/> public class integerutils {</P> <p> static final int max_cached= 2048; </P> <p> Private Static integer [] cache = new integer [max_cached]; <br/> Private Static string [] cachedstrings = new string [max_cached]; <br/> Private Static string [] cachedhexstrings = new string [max_cached]; </P> <p>/** <br/> * implement the same logic as integer. valueof (INT) in Java 1.5. <br/> * @ Param I <br/> * @ return <br/> */<br/> Public static integer getinteger (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cache) {<br/> If (Cache [I] = NULL) {<br/> cache [I] = new INTEGER (I ); <br/>}< br/> return cache [I]; <br/>}else {<br/> return New INTEGER (I ); <br/>}</P> <p> Public static string tostring (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cachedstrings) {<br/> If (cachedstrings [I] = NULL) {<br/> cachedstrings [I] = integer. tostring (I); <br/>}< br/> return cachedstrings [I]; <br/>} else {<br/> return integer. tostring (I); <br/>}</P> <p> Public static string tohexstring (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cachedhexstrings) {<br/> If (cachedhexstrings [I] = NULL) {<br/> cachedhexstrings [I] = integer. tohexstring (I); <br/>}< br/> return cachedhexstrings [I]; <br/>}else {<br/> return integer. tohexstring (I ); <br/>}</P> <p>/** <br/> * parseint that never throws exceptions <br/> * @ Param STR <br/> * @ return <br/> */<br/> Public static int parseint (string Str) {<br/> try {<br/> return integer. parseint (STR); <br/>} catch (exception e) {<br/> return 0; <br/>}</P> <p> Public static int parseint (string STR, int Radix) {<br/> try {<br/> return integer. parseint (STR, Radix); <br/>}catch (exception e) {<br/> return 0; <br/>}< br/>}