C # Performance Comparison of converting a string to a String Array

Source: Internet
Author: User

comparison of four methods:

Private void form2_load (Object sender, eventargs e) <br/>{< br/> string STR = "this mood is like the low-calorie ice cream. It's not appealing enough, I always think something is missing. "<br/> string [] A; </P> <p> skyvu. tester tester = new skyvu. tester (100000); <br/> richtextbox1.appendtext (tester. run ("tochararray", delegate () <br/>{< br/> A = splittoarray (STR); </P> <p> })); </P> <p> richtextbox1.appendtext (tester. run ("stringindex", delegate () <br/>{< br/> A = splittoarray 2 (STR); </P> <p >}); </P> <p> richtextbox1.appendtext (tester. run ("RegEx", delegate () <br/>{< br/> A = splittoarray3 (STR); </P> <p> })); </P> <p> richtextbox1.appendtext (tester. run ("LINQ", delegate () <br/>{< br/> A = splittoarray4 (STR); </P> <p> })); </P> <p >}</P> <p> Public String [] splittoarray (string Str) <br/>{< br/> char [] CHS = Str. tochararray (); <br/> string [] Sarr = new string [chs. length]; <br/> Fo R (INT I = 0; I <chs. length; I ++) <br/>{< br/> // Sarr [I] = CHS [I]. tostring (); <br/> Sarr [I] = new string (CHS [I], 1); <br/>}< br/> return Sarr; <br/>}</P> <p> Public String [] splittoarray2 (string Str) <br/>{< br/> string [] Sarr = new string [Str. length]; <br/> for (INT I = 0; I <Str. length; I ++) <br/>{< br/> Sarr [I] = STR [I]. tostring (); <br/>}< br/> return Sarr; <br/>}< br/> // Private Static RegEx regsplit = new RegEx ("(?! ^ | $) ", Regexoptions. compiled); // no significant improvement <br/> Public String [] splittoarray3 (string Str) <br/>{< br/> return RegEx. split (STR ,"(?! ^ | $) "); <Br/> // return regsplit. split (STR); <br/>}</P> <p> Public String [] splittoarray4 (string Str) <br/>{< br/> return (from CH in STR select ch. tostring ()). toarray (); <br/>}

Tester class source code

 

Result:

Tochararray: 128 Ms <br/> stringindex: 135 ms <br/> RegEx: 2480 Ms <br/> LINQ: 469 MS

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.