Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To test the suggestion by Romoku of using <code>StringBuilder</code> I have produced the following code.</p> <pre><code> public static void Reverse(string x) { string text = x; string reverse = string.Empty; foreach (char ch in text) { reverse = ch + reverse; } Console.WriteLine(reverse); } public static void ReverseFast(string x) { string text = x; StringBuilder reverse = new StringBuilder(); for (int i = text.Length - 1; i &gt;= 0; i--) { reverse.Append(text[i]); } Console.WriteLine(reverse); } public static void Main(string[] args) { int abcx = 100; // amount of abc's string abc = ""; for (int i = 0; i &lt; abcx; i++) abc += "abcdefghijklmnopqrstuvwxyz"; var x = new System.Diagnostics.Stopwatch(); x.Start(); Reverse(abc); x.Stop(); string ReverseMethod = "Reverse Method: " + x.ElapsedMilliseconds.ToString(); x.Restart(); ReverseFast(abc); x.Stop(); Console.Clear(); Console.WriteLine("Method | Milliseconds"); Console.WriteLine(ReverseMethod); Console.WriteLine("ReverseFast Method: " + x.ElapsedMilliseconds.ToString()); System.Console.Read(); } </code></pre> <p>On my computer these are the speeds I get per amount of alphabet(s).</p> <p>100 ABC(s) Reverse ~5-10ms FastReverse ~5-15ms</p> <p>1000 ABC(s) Reverse ~120ms FastReverse ~20ms</p> <p>10000 ABC(s) Reverse <strong>~16,852ms!!!</strong> FastReverse ~262ms</p> <p>These time results will vary greatly depending on the computer but one thing is for certain if you are processing more than 100k characters you are insane for not using <code>StringBuilder</code>! On the other hand if you are processing less than 2000 characters the overhead from the <code>StringBuilder</code> definitely seems to catch up with its performance boost.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload