Optimization of Java strings

Source: Internet
Author: User

Java Program Optimization: string manipulation, basic operation methods and other optimization strategies
Read this document, slightly feel, so this is a review of the book. And the previous Java a way to increase the output speed further.
It mainly involves 7 kinds of optimization methods.
1. subString(begin,end):
SUBSTRING is a way to intercept a paragraph in the string, I have been suffering on this, I have done a large amount of data substring has been oom, did not find a solution, today I see the text mentioned if the use of

returnnewString(string.substring(begin,end))代替returnstring.substring(begin,end)

Because a new string object is generated to make the memory leaked by substring () A string object that is returned by the loss of a strong reference and is recycled to ensure stability, but, I knocked a few test samples have not been proved, hen whty, still need to verify.
2. 使用StringBuilder|StringBuffer进行string连接
See the blog I wrote earlier, using StringBuilder is really string1+string2的方式 faster, can write test validation, and also mentions the use of StringBuffer in multithreading is more secure than StringBuilder.
3. 局部变量的比类变量的计算速度快
Well, the general local variables and class global variables are set first to consider not speed, so this is good to know.
4. 位运算速度快于乘除法
RT, there's nothing to say.
5. if-else优于switch
It depends on the design of the structure.
6. 提取表达式
Pick a sample program:

 Public classDuplicatedcode { Public Static void beforetuning(){LongStart = System.currenttimemillis ();DoubleA1 = Math.random ();DoubleA2 = Math.random ();DoubleA3 = Math.random ();Doublea4 = Math.random ();DoubleB1,B2; for(intI=0;i<10000000; i++) {B1 = a1*a2*a4/3*4*A3*A4; B2 = a1*a2*a3/3*4*A3*A4; } System. out. println (System.currenttimemillis ()-start); } Public Static void aftertuning(){LongStart = System.currenttimemillis ();DoubleA1 = Math.random ();DoubleA2 = Math.random ();DoubleA3 = Math.random ();Doublea4 = Math.random ();DoubleCOMBINE,B1,B2; for(intI=0;i<10000000; i++) {combine = a1*a2/3*4*A3*A4; B1 = combine*a4; b2 = combine*a3; } System. out. println (System.currenttimemillis ()-start); } Public Static void Main(string[] args) {duplicatedcode.beforetuning (); duplicatedcode.aftertuning ();}}

After extraction, the running time is less than half, mainly due to the lack of repeated calculations.
7.减少循环次数

fori=0;i<999999;i{        array[i]=i;}
for(int i=0;i<999999;i+=3) {    array[i]=i;    array[i+1]=i+1;    array[i+2]=i+2;}

Which is faster? Guess it's clear.
8. system.arrayCopy()
It's fast because it's native, many functions non-native speed is not necessarily faster than the assignment of your one by a!

SUBSTRING () This still puzzled, who understand, please advise, kneeling Xie

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Optimization of Java strings

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.