Speed tests on Sting and StirngBuilder

Use the Stopwatch timer class (note references to namespace system.diagnostics;) Method, Start() to Start the time, Stop() to Stop the time, and property Elapsed: Returns the time interval from Start to finish

Then do the same for the Sting variable and the Stringbuilder object. Here I’m asking them to add 50000 characters.

  • String time: about 1.6s
  • StringBuilder time: about 0.007s
using System; using System.Diagnostics; using System.Text; Class Program {static void Main(string[] args) {Stopwatch sw = new Stopwatch(); sw.Start(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 50000; i++) { sb.Append(i); } sw.Stop(); Console.WriteLine(sw.Elapsed); Console.ReadKey(); }}}Copy the code

Run with StringBuilder result: \

using System; using System.Diagnostics; using System.Text; Class Program {static void Main(string[] args) {Stopwatch sw = new Stopwatch(); sw.Start(); string num = ""; for (int i = 0; i < 50000; i++) { num += i; } sw.Stop(); Console.WriteLine(sw.Elapsed); Console.ReadKey(); }}}Copy the code

Run the result with String: \



Brief introduction of common methods:

CompareTo() : CompareTo() : CompareTo() : CompareTo(); Small returns -1) 2,Replace() replaces the given character in the string with another character 3,Split() splits the string into an array of strings where the given character is present (splits the string according to (arguments), Form a new string) 4,SubString() retrieves a SubString at a given position in the string (intercepting a string returns a SubString, two overloads) 5,ToLower() converts the string to lowercase 6,ToUpper() converts the string to uppercase 7,Trim() removes whitespace at the beginning and end (e.g. : 8,Concat(), merge string 9,CopyTo(), copy the specified string into an array 10,Format(),Format string 11,IndexOf(), Returns the first occurrence of a given string or character in a string (returns the first occurrence of a string, or -1 if it does not exist) 12,Insert() inserts an instance of a string into the specified index of another instance of a string 13,Join() joins an array of strings, Create a new string

StringBuilder (note references to namespace system.text)

  1. Create a StringBuilder object
/ / = = = = = = = = = = = = = = = = = = StringBuilder = = = = = = = = = = = = = = = = = = / / use the constructor to create the StringBuilder sb = new StringBuilder (" Czhenya "); Sb1 = new StringBuilder(12); Sb2 = new StringBuilder("Czhenya", 20); // If the initial space is insufficient, a new space will be requested, which is twice the size of the original spaceCopy the code

About the memory footprint of StringBuilder object creation

  1. The Append() method appends the current string with one character
  2. Insert() appends a string of a particular format
  3. Remove() removes characters from the current string
  4. Replace() Replaces one character or string entirely with another character or string in the current string
  5. ToString() extracts the string currently stored in stringBuilder into an immutable string