Efficiency analysis of string concatenation in C #

Source: Internet
Author: User

Tag: SYS operation memory allocation effect result opened OOP code extra

Code tested with Unity Profiler, and run results

usingUnityengine;usingunityengine.profiling;usingSystem.Text;#ifUnity_5_5_or_newerusingTprofiler =UnityEngine.Profiling.Profiler;#elseusingTprofiler =Unityengine.profiler;#endif Public namespaceTest {///string concat test     Public classTeststring:monobehaviour {//Update is called once per frame        voidUpdate () {//Loop 10000times             for(inti =0; I <10000; ++i) {teststring ();        } TestString10000 (); }        //Object New Once        PrivateStringBuilder _SB =NewStringBuilder (); ///concat 10000 times        Private voidTestString10000 () {// +Tprofiler.beginsample ("string. (10000+)"); stringS1 =string.            Empty;  for(inti =0; I <10000; ++i) {S1= S1 + ((int) (time.time).            ToString ();            } tprofiler.endsample (); //StringBuilder AppendTprofiler.beginsample ("Stringbuilder.10000append"); _SB.            Clear ();  for(inti =0; I <10000; ++i) {_sb. Append ((int) time.time); }            stringS2 =_SB.            ToString ();        Tprofiler.endsample (); }        ///concat 3 Parts        Private voidteststring () {system.random Random=NewSystem.Random (); // +Tprofiler.beginsample ("string. (+)"); stringS1 ="txt_pre_"+ Random. Next (). ToString () +"_end";            Tprofiler.endsample (); //ConcatTprofiler.beginsample ("string. Concat"); stringS2 =string. Concat ("txt_pre_", Random. Next (),"_end");            Tprofiler.endsample (); //Concat ToString ()Tprofiler.beginsample ("string. Concat & Int. ToString"); stringS3 =string. Concat ("txt_pre_", ((int) (Time.time). ToString (),"_end");            Tprofiler.endsample (); //formatTprofiler.beginsample ("string. Format"); stringS4 =string. Format ("Txt_pre_{0}_end", (int) time.time);            Tprofiler.endsample (); //format ToString ()Tprofiler.beginsample ("string. Format & Int. ToString"); stringS5 =string. Format ("Txt_pre_{0}_end", ((int) (time.time).            ToString ());            Tprofiler.endsample (); //StringBuilder AppendFormatTprofiler.beginsample ("Stringbuilder.appendformat & Int. ToString"); _SB.            Clear (); _SB. AppendFormat ("Txt_pre_{0}_end", ((int) (time.time).            ToString ()); stringS6 =_SB.            ToString ();            Tprofiler.endsample (); //StringBuilder AppendTprofiler.beginsample ("Stringbuilder.append"); _SB.            Clear (); _SB. Append ("txt_pre_"); _SB. Append ((int) time.time); _SB. Append ("_end"); stringS7 =_SB.            ToString ();            Tprofiler.endsample (); //StringBuilder Append ToString ()Tprofiler.beginsample ("stringbuilder.append & Int. ToString"); _SB.            Clear (); _SB. Append ("txt_pre_"); _SB. Append ( (int) (time.time).            ToString ()); _SB. Append ("_end"); stringS8 =_SB.            ToString ();            Tprofiler.endsample (); //StringBuilder AppendnumberTprofiler.beginsample ("Stringbuilder.appendnumber"); _SB.            Clear (); _SB. Append ("txt_pre_"); _SB. Appendnumber ((int) time.time); _SB. Append ("_end"); stringS9 =_SB.            ToString ();        Tprofiler.endsample (); }    }}
View Code

Operation Result:

Results Analysis and conclusion:
1, String. Format and Stringbuider.appendformat ()

Good readability
High heap memory overhead because format requires additional memory allocations
Low operating efficiency
Suggestions:
"Less use, use with caution"
2, Stringbuilder.append

General readability
Low heap memory overhead
High operational efficiency
Suggestions:
Large number of strings splicing "must use", memory and operational efficiency optimization effect is very obvious
"Use as appropriate" when a small number of strings are spliced, such as frequency of calls
3, + and concat

Good readability
Low heap memory overhead
High operational efficiency
In many cases, the + will be optimized to concat
Suggestions:
A large number of string concatenation "Prohibit use", memory and operational efficiency is very expensive, unbearable
"Recommended" When a small number of strings are spliced
In addition, for the basic data types such as int for string concatenation, develop the habit of ToString () to avoid the extra memory overhead of the box operation.

Efficiency analysis of string concatenation in C #

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.