Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: the top efficiency in converting an int[] into a string
    primarykey
    data
    text
    <p>I'm posting a new thread since the last one was very confused and the idea has been modified as well. I changed the program in many places, not losing efficiency (even gaining a bit) and now I have a simple array of integers as before, no separators needed.</p> <p><a href="https://stackoverflow.com/questions/13440323/c-the-most-efficient-way-to-convert-int-into-a-string">Previous thread for the reference</a></p> <p>I do know that this kind of questions have already been answered many times. Although I've found lots of possible answers, they still don't solve my problem, <strong>which is to implement the fastest possible way to convert an integer array into a single string</strong>.</p> <p>All right then, </p> <pre><code>int[] Result = new int[] { 636, 1000234545, 1353678530, 987001 } </code></pre> <p><strong>I should get:</strong></p> <pre><code>636000234545353678530987001 </code></pre> <p>Notice that I have taken only the last 9 digits of each element. Here's corrected version of Honza Brestan:</p> <pre><code>StringBuilder sb = new StringBuilder(); for (var i = 0; i &lt; xC; i++) { tempint = Result[i]; if (tempint &gt; 999999999) sb.Append((Result[i]).ToString().Substring(1, 9)); else sb.Append((Result[i]).ToString()); } return sb.ToString(); </code></pre> <p>And my, old, corrected:</p> <pre><code>//Base – a string array of integers saved as strings {“000”, “001”, … , “999” } string[] arr = new string[3 * limit]; int x; // temp value for (int i = 0; i &lt; limit; i++) { x = Result[i]; if (x &gt; 999999) { arr [3 * i + 2] = Base [x % 1000]; arr [3 * i + 1] = Base [x / 1000 % 1000]; arr [3 * i] = Base [x / 1000000 % 1000]; } else { if (x &lt; 1000) { arr [3 * i + 2] = Base [x % 1000]; } else { arr [3 * i] = Base [x / 1000 % 1000]; arr [3 * i + 1] = Base [x % 1000]; } } } return string.Join(null, arr); </code></pre> <p>And now difference in speed: Honza: <strong>689 ms</strong> My: <strong>331 ms</strong></p> <p>Any ideas how to improve the speed? Maybe use assembler?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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