Introducedoften the client calls and complains: Your program is slow as a snail. You start checking for possible suspects: File IO, database access speed, and even viewing Web services. But these possible doubts are normal, not a problem at all.You use the most convenient performance analysis tool analysis, found that the bottleneck is a small function, the function is to write a long string list into a file.You have optimized this function by connecting all the small strings into a long string,
Do the project often use string and stringbuilder,string can be used "+" to the string concatenation, StringBuilder with append for stitching, never understand since you can use string, Ask what you want with StringBuilder. Although in the database query, the habitual use of the StringBuilder to the query statement sti
Tring is a string constant. StringBuffer is a string variable, thread-safe. StringBuilder is a string variable and the thread is not secure.
Speed: String
Why is string speed the slowest?
Because each time a string type is changed, it is equivalent to generating a new string object and then pointing the pointer at a new string object, so that the old object accumulates in memory when the new object is constantly generated, causing memory waste, The J
When you modify a string, you need to use the StringBuffer and StringBuilder classes.Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified more than once, and no new unused objects are produced.The StringBuilder class is presented in Java 5, and the biggest difference between it and StringBuffer is that the
. currenttimemillis ();String STR = "";For (INT I = 0; I Stringbuffer sb = new stringbuffer (STR );SB. append (tempstr );STR = sb. tostring ();}Long lend2 = system. currenttimemillis ();Long time2 = (lend2-lstart2 );System. Out. println (time2 );The average execution time is about 46922, that is, 46 seconds.
Else ---------------------------------------------------------------------------------------------------------------------------------
Stringbuffer maintains a fixed-size string buffer. When
Ms)
VaR
Str
=
""
;
For
(
VaR
I
=
0
; I
10000
; I
++
)
{Str+ ="ABC";}
Method 2-Array method (execution time:62 Ms)
VaR
Str
=
""
;
VaR
Arr
=
New
Array ();
For
(
VaR
I
=
0
; I
10000
; I
++
)
{Arr. Push ("ABC");}
Str
=
Arr. Join ('');
We can see that the code execution efficiency of the second method has been greatly improved. After careful analysis, we will find that method 2 reduce
Introduction:Some major operations, especially in the client browser, were considered very difficult not long ago. Nowadays, various development applications made by popular Ajax have quickly become a popular technology. However, most browsers on the market do not handle string connections very quickly.The good news is that although ie processes string connections slowly, character operations are already the fastest. For this reason, I decided to write a simple
. When the string size does not exceed the capacity, no new capacity is allocated. When the string size exceeds the capacity, the capacity is automatically increased.
Stringbuffer Buf = new stringbuffer (); // allocate a 16-Byte Character Buffer stringbuffer Buf = new stringbuffer (512 ); // allocate a 512-Byte Character Buffer stringbuffer Buf = new stringbuffer ("this is a test") // stores strings in the buffer, A 16-byte empty buffer is reserved later.
The string buffer can be safely used for
Difference between String, StringBuffer and StringBuilder, stringbufferDirectory:
1. Immutable String class
2. What should I pay attention to when using String?
3. Differences between String and StringBuilder
4. Differences between StringBuilder and StringBuffer
Immutable String class:
For this non-variability, you need to check the source code. There are several
In peacetime development, we may encounter strings that need to be spliced in the following format (at least I have encountered many times):1,2,3,4,5,6,7,8,9,10,11,12,12,12,12,34,234,2134,1234,1324,1234,123The character of this string: Multiple data is separated by a special symbol.I used to think so, and I believe a lot of people are thinking the same way:public class stringtest{public static void Main (string[] args) { StringBuilder sb
Similarities:
Both objects are string operations,
Differences:
String typeEach time the string is referenced, the space will be allocated again, and the originally allocated space will also exist. If you repeat the operation, it will occupy a large amount of memory space, thus wasting memory resources, such:
String str = "life ";
Str = str + "you ";
First, a str variable is defined to allocate physical memory space for the string "Lifetime,
The str value is now "you are in your life", and a ph
Differences in string, StringBuffer, StringBuilder in Java (RPM)In Java, String, StringBuffer, and StringBuilder are frequently used strings in programming, and the differences between them are frequently asked questions in the interview. Now summarize and see how they differ from the same.1. Variable and non-changeable The string class uses a character array to hold strings, as follows, because there is a
The StringBuilder does not provide a clear or empty method.There are 3 ways to clear:1) Reborn into one, the old by the system itself voluntarily recycled2) Use Delete3) Use SetLengthCycle three methods 10 million times, code:1.public class SBBM {2.3. Static String A;4. Static long time;5. public static void Main (string[] args) throws Exception {6.7. StringBuilder sb = new
follows:
--------------------------
10 times bfindexof= 94ms
10 times stringindexof= 56ms
10 times kmpindexof= 76ms
10 times allbuild= 5870ms
10 times alltoarray= 137ms
10*3 times, gc= 155ms
10 times, all total time consuming: 6388ms; loss: 6007ms; loss ratio: 94.03569192235442%
--------------------------
30 times bfindexof= 365ms
30 times stringindexof= 222ms
30 times kmpindexof= 295ms
30 times allbuild= 16452ms
30 times alltoarray= 395ms
30*3 times gc= 452ms
30 times all total time co
Why to use StringBuilder
Why use StringBuilder to start with the properties of a string object.
string object when string stitching, Because of the immutability of the string, the string object will be duplicated every time, copying a copy out of the operation, and its own strings remain in memory, a large number of temporary fragments will cause negligible performance loss. Therefore, it i
This article mainly introduces the StringBuilder effect of js to implement C #, analyzes and summarizes the skills related to the StringBuilder effect of js to implement C # in the form of a complete example, and has some reference value, for more information about how to implement StringBuilder in js, see the following example. We will share this with you for yo
String is a reference type, but it also has someValueType features.
String indicates whether to pass the value or reference. The string declaration of c # Is a string class. Of course, it is to pass the reference. However, most of these questions are due to this situation:String A = "AAA ";String B =;B = "BBB ";
At this time, the output result shows that the value of a has not changed. analyze it as follows:
String A = "AAA"; // A --> New String ("AAA ")String B = A; // B --> A, upload ref
Just two are looking, found that there is an author upload a program (estimated that the original program author also did not run directly into the, I directly paste the run when several errors, I have modified a bit), the programclassPreson { Public stringFirstName; Public stringMiddleName; Public stringLastName;//define 3 Private variables FirstName middlename lastName /*Public Preson (string fn,string mn,string ln)//constructor {FIRSTNAME=FN; MIDDLENAME=MN;
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.