Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using a StringBuilder to assemble the string will get you a 600 times increase in processing (as it avoids repeated object creation everytime you append to the string.</p> <p>before loop (initialising capacity avoids recreating the backing array in StringBuilder):</p> <pre><code>StringBuilder sb = new StringBuilder(1000000 * ReadLength); </code></pre> <p>in loop:</p> <pre><code>sb.Append(all.Substring(randomPos, ReadLength) + Environment.NewLine); </code></pre> <p>after loop:</p> <pre><code>readString = sb.ToString(); </code></pre> <p>Using a char array instead of a string to extract the values yeilds another 30% improvement as you avoid object creation incurred when calling Substring():</p> <p>before loop:</p> <pre><code>char[] chars = all.ToCharArray(); </code></pre> <p>in loop:</p> <pre><code>sb.Append(chars, randomPos, ReadLength); sb.AppendLine(); </code></pre> <p><strong>Edit (final version which does not use StringBuilder and executes in 300ms):</strong></p> <pre><code>char[] chars = all.ToCharArray(); var iterations = 1000000; char[] results = new char[iterations * (ReadLength + 1)]; GetRandomStrings(len, iterations, ReadLength, chars, results, 0); string s = new string(results); private static void GetRandomStrings(int len, int iterations, int ReadLength, char[] chars, char[] result, int resultIndex) { Random random = new Random(); int i = 0, index = resultIndex; while (i &lt; iterations &amp;&amp; len - 100 &gt; 0) //len is 3000000 { var i1 = len - ReadLength; int randomPos = random.Next() % i1; Array.Copy(chars, randomPos, result, index, ReadLength); index += ReadLength; result[index] = Environment.NewLine[0]; index++; i++; } } </code></pre>
 

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