<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Effektivster Weg, unsigned int in string umzuwandeln]]></title><description><![CDATA[<p>Hallo, ich schreibe gerade eine Anwendung, in der ich ein unsigned int in einen string umwandeln muss. Normalerweise mache ich das immer so:</p>
<pre><code class="language-cpp">std::stringstream Str;
Str&lt;&lt;MeineZahl;
Str&gt;&gt;Mein_std_string;
</code></pre>
<p>Das ist zuverlässig, bietet manipulatoren und ist sicher. Allerdings brauche ich jetzt eine Funktion, die diese umwandlung so schnell wie möglich macht. Dabei ist es mir egal, ob die Zahl als std::string oder C-String vorliegt (was halt schneller ist). Zuerst dachte ich an <strong>atoi</strong>, allerdings weis ich nicht, wie diese Funktion intern funktioniert.<br />
Dann wollte ich mir das selber schreiben:</p>
<pre><code class="language-cpp">unsigned int zahl;
std::string str(10, '0');
str[0]=static_cast&lt;char&gt;(zahl/1000000000+'0');
str[1]=static_cast&lt;char&gt;(zahl/100000000+48);
str[2]=static_cast&lt;char&gt;(zahl/10000000+48);
//usw
</code></pre>
<p>Allerdings weis ich nicht, ob die ganzen Divisionen bei der Performance stören.<br />
Welche Methode würdet ihr wählen? Gibt es eine &quot;Standard Vorgehensweise&quot; für die performante Umwandlung von Zahlen in strings? Denn irgendwie muss der Computer aus den Bitmustern ja auch lesbare ASCII Zeichen machen.</p>
<p>Schonmal Danke im Voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/252367/effektivster-weg-unsigned-int-in-string-umzuwandeln</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 11:27:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/252367.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Oct 2009 21:32:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:32:23 GMT]]></title><description><![CDATA[<p>Hallo, ich schreibe gerade eine Anwendung, in der ich ein unsigned int in einen string umwandeln muss. Normalerweise mache ich das immer so:</p>
<pre><code class="language-cpp">std::stringstream Str;
Str&lt;&lt;MeineZahl;
Str&gt;&gt;Mein_std_string;
</code></pre>
<p>Das ist zuverlässig, bietet manipulatoren und ist sicher. Allerdings brauche ich jetzt eine Funktion, die diese umwandlung so schnell wie möglich macht. Dabei ist es mir egal, ob die Zahl als std::string oder C-String vorliegt (was halt schneller ist). Zuerst dachte ich an <strong>atoi</strong>, allerdings weis ich nicht, wie diese Funktion intern funktioniert.<br />
Dann wollte ich mir das selber schreiben:</p>
<pre><code class="language-cpp">unsigned int zahl;
std::string str(10, '0');
str[0]=static_cast&lt;char&gt;(zahl/1000000000+'0');
str[1]=static_cast&lt;char&gt;(zahl/100000000+48);
str[2]=static_cast&lt;char&gt;(zahl/10000000+48);
//usw
</code></pre>
<p>Allerdings weis ich nicht, ob die ganzen Divisionen bei der Performance stören.<br />
Welche Methode würdet ihr wählen? Gibt es eine &quot;Standard Vorgehensweise&quot; für die performante Umwandlung von Zahlen in strings? Denn irgendwie muss der Computer aus den Bitmustern ja auch lesbare ASCII Zeichen machen.</p>
<p>Schonmal Danke im Voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794484</guid><dc:creator><![CDATA[Gilder]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:32:23 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:36:01 GMT]]></title><description><![CDATA[<p>//EDIT:<br />
Ersetze <strong>atoi</strong> im Text oben mit <strong>itoa</strong> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794487</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794487</guid><dc:creator><![CDATA[Gilder]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:36:01 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:36:50 GMT]]></title><description><![CDATA[<p>Gilder schrieb:</p>
<blockquote>
<p>Allerdings weis ich nicht, ob die ganzen Divisionen bei der Performance stören.</p>
</blockquote>
<p>Natürlich stören sie.<br />
<a href="http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25112.PDF" rel="nofollow">http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25112.PDF</a><br />
Punkt 8.7</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794488</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794488</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:36:50 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:37:49 GMT]]></title><description><![CDATA[<p>so:</p>
<pre><code class="language-cpp">std::stringstream ss;
ss &lt;&lt; number;
ss &gt;&gt; string;
</code></pre>
<p>ich bin mir zwar nicht sicher, wie man es am besten machen kann, aber ich denke fast, dass es nen weg mit weniger divisionen gibt^^ vll mit irgendwelchem bit-gefrickel, aber kann mir nur schwer vorstellen, dass stringstream so extrem viel langsamer sein soll, wie wenn mans selbst machen wöllte...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794491</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:37:49 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:45:19 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>ich bin mir zwar nicht sicher, wie man es am besten machen kann, aber ich denke fast, dass es nen weg mit weniger divisionen gibt^^ vll mit irgendwelchem bit-gefrickel, aber kann mir nur schwer vorstellen, dass stringstream so extrem viel langsamer sein soll, wie wenn mans selbst machen wöllte...</p>
</blockquote>
<p>Kann ich mir aber schon vorstellen. Also ich könnte mir vorstellen, daß dauernd die aktuellen loales befragt werden, daß streamflags wie width und fill befragt werden, halt erstmal möglichst viel zeugs, das die sache flexibler und langsamer macht. und der fetteste brocken ist vermutlich der auf dem freispeicher angelegte puffer des stringstreams. klingt nach 100 takten grundgebühr und um 5% höhere ziffernkosten, als man per hand hinkriegt.</p>
<p>aber fast immer ist es völlig egal, weil die zahl dann auf den bildschirm zu bringen nochmal das zehntausendfache kostet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794493</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794493</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:45:19 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:48:02 GMT]]></title><description><![CDATA[<p>Wäre <strong>sprintf(str,&quot;%u&quot;,value)</strong> für euch eine Alternative? Würde das besser abschneiden als über stringstreams?<br />
Und wie soll bitte das &quot;Bitgefrickel&quot; aussehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794497</guid><dc:creator><![CDATA[Gilder]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:48:02 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:52:21 GMT]]></title><description><![CDATA[<p>Gilder schrieb:</p>
<blockquote>
<p>Und wie soll bitte das &quot;Bitgefrickel&quot; aussehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Das was volkard zuerst geposted hat. Wobei das soooo frickelig gar nicht ist, wenn man sich mal die Zeit nimmt, den Algorithmus nachzuvollziehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794499</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:52:21 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:54:09 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>und der fetteste brocken ist vermutlich der auf dem freispeicher angelegte puffer des stringstreams. klingt nach 100 takten grundgebühr und um 5% höhere ziffernkosten, als man per hand hinkriegt.</p>
</blockquote>
<p>konnte man den nicht noch irgendwie manuell festlegen?!<br />
sollten ja 11 stellen reichen...</p>
<p>die sache mit den flags ist wohl aber nicht von der hand zu weisen^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794500</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794500</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:54:09 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 21:54:31 GMT]]></title><description><![CDATA[<p>Assemblercode verstehe ich nicht. Da muss ich wohl jemanden finden, der mir das in C++ &quot;übersetzt&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794501</guid><dc:creator><![CDATA[Gilder]]></dc:creator><pubDate>Sun, 18 Oct 2009 21:54:31 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:05:12 GMT]]></title><description><![CDATA[<p>Gilder schrieb:</p>
<blockquote>
<p>Wäre <strong>sprintf(str,&quot;%u&quot;,value)</strong> für euch eine Alternative? Würde das besser abschneiden als über stringstreams?</p>
</blockquote>
<p>Das parsen des format-strings dauert auch.</p>
<p>Gilder schrieb:</p>
<blockquote>
<p>Und wie soll bitte das &quot;Bitgefrickel&quot; aussehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Normalerweise im Kern nur</p>
<pre><code class="language-cpp">do{
   *writepos=x%10;
   ++writepos;
}while(x/=10);
//umdrehen
//zurückgeben
</code></pre>
<p>Und dann hat man die Wahl, ob man eine static array nimmt für die Rückgabe, was den Vorteil hat, daß man rückwärts schreiben kann, ob man den String danach umdreht oder ob man per Rekursion rückwärts schreibt. Und Ob man versucht Tabellenguckerchen zu machen umd Divisionen zu sparen. Und ob man gleich asm leut Buch benutzt.</p>
<p>Ich benutze nur Rekursion zum Umdrehen und keine weiteren Tricks, ist mir schnell genug so im Moment. Ich hab's aber nicht direkt der Geschwindigkeit wegen gemacht, sondern eher, ums gemacht zu haben.</p>
<pre><code class="language-cpp">ostream&amp; operator&lt;&lt;(ostream&amp; out,unsigned int x){
   if(n&gt;=10)
      out&lt;&lt;n/10;
   return out&lt;&lt;char('0'+n%10);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1794503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794503</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:05:12 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:00:07 GMT]]></title><description><![CDATA[<p>Gilder schrieb:</p>
<blockquote>
<p>Assemblercode verstehe ich nicht. Da muss ich wohl jemanden finden, der mir das in C++ &quot;übersetzt&quot;</p>
</blockquote>
<pre><code class="language-asm">__declspec(naked) void __stdcall uint_to_ascii_lz(char *sptr, unsigned int x)
{
  __asm {
    push edi ; Save as per calling conventions.
    push esi ; Save as per calling conventions.
    push ebx ; Save as per calling conventions.
    mov eax, [esp+20] ; x
    mov edi, [esp+16] ; sptr
    mov esi, eax ; x
    mov edx, 0xA7C5AC47 ; Divide x by
    mul edx ; 10,000 using
    add eax, 0xA7C5AC47 ; multiplication
    adc edx, 0 ; with reciprocal.
    shr edx, 16 ; y1 = x / 1e5
    mov ecx, edx ; y1
    imul edx, 100000 ; (x / 1e5) * 1e5
    sub esi, edx ; y2 = x % 1e5
    mov eax, 0xD1B71759 ; 2^15 / 1e4 * 2^30
    mul ecx ; Divide y1 by 1e4,
    shr eax, 30 ; converting it into
    lea ebx, [eax+edx*4+1] ; 17.15 fixed-point format
    mov ecx, ebx ; such that 1.0 = 2^15.
    mov eax, 0xD1B71759 ; 2^15 / 1e4 * 2^30
    mul esi ; Divide y2 by 1e4,
    shr eax, 30 ; converting it into
    lea esi, [eax+edx*4+1] ; 17.15 fixed-point format
    mov edx, esi ; such that 1.0 = 2^15.
    shr ecx, 15 ; 1st digit
    and ebx, 0x00007fff ; Fraction part
    or ecx, '0' ; Convert 1st digit to ASCII.
    mov [edi+0], cl ; Store 1st digit in memory.
    lea ecx, [ebx+ebx*4] ; 5 * fraction, new digit ECX[31-14]
    lea ebx, [ebx+ebx*4] ; 5 * fraction, new fraction EBX[13-0]
    shr edx, 15 ; 6th digit
    and esi, 0x00007fff ; Fraction part
    or edx, '0' ; Convert 6th digit to ASCII.
    mov [edi+5], dl ; Store 6th digit in memory.
    lea edx, [esi+esi*4] ; 5 * fraction, new digit EDX[31-14]
    lea esi, [esi+esi*4] ; 5 * fraction, new fraction ESI[13-0]
    shr ecx, 14 ; 2nd digit
    and ebx, 0x00003fff ; Fraction part
    or ecx, '0' ; Convert 2nd digit to ASCII.
    mov [edi+1], cl ; Store 2nd digit in memory.
    lea ecx, [ebx+ebx*4] ; 5 *f raction, new digit ECX[31-13]
    lea ebx, [ebx+ebx*4] ; 5 * fraction, new fraction EBX[12-0]
    shr edx, 14 ; 7th digit
    and esi, 0x00003fff ; Fraction part
    or edx, '0' ; Convert 7th digit to ASCII.
    mov [edi+6], dl ; Store 7th digit in memory.
    lea edx, [esi+esi*4] ; 5 * fraction, new digit EDX[31-13]
    lea esi, [esi+esi*4] ; 5 * fraction, new fraction ESI[12-0]
    shr ecx, 13 ; 3rd digit
    and ebx, 0x00001fff ; Fraction part
    or ecx, '0' ; Convert 3rd digit to ASCII.
    mov [edi+2], cl ; Store 3rd digit in memory.
    lea ecx, [ebx+ebx*4] ; 5 * fraction, new digit ECX[31-12]
    lea ebx, [ebx+ebx*4] ; 5 * fraction, new fraction EBX[11-0]
    shr edx, 13 ; 8th digit
    and esi, 0x00001fff ; Fraction part
    or edx, '0' ; Convert 8th digit to ASCII.
    mov [edi+7], dl ; Store 8th digit in memory.
    lea edx, [esi+esi*4] ; 5 * fraction, new digit EDX[31-12]
    lea esi, [esi+esi*4] ; 5 * fraction, new fraction ESI[11-0]
    shr ecx, 12 ; 4th digit
    and ebx, 0x00000fff ; Fraction part
    or ecx, '0' ; Convert 4th digit to ASCII.
    mov [edi+3], cl ; Store 4th digit in memory.
    lea ecx, [ebx+ebx*4] ; 5 * fraction, new digit ECX[31-11]
    shr edx, 12 ; 9th digit
    and esi, 0x00000fff ; Fraction part
    or edx, '0' ; Convert 9th digit to ASCII.
    mov [edi+8], dl ; Store 9th digit in memory.
    lea edx, [esi+esi*4] ; 5 * fraction, new digit EDX[31-11]
    shr ecx, 11 ; 5th digit
    or ecx, '0' ; Convert 5th digit to ASCII.
    mov [edi+4], cl ; Store 5th digit in memory.
    shr edx, 11 ; 10th digit
    or edx, '0' ; Convert 10th digit to ASCII.
    mov [edi+9], dx ; Store 10th digit and end marker in memory.
    pop ebx ; Restore register as per calling convention.
    pop esi ; Restore register as per calling convention.
    pop edi ; Restore register as per calling convention.
    ret 8 ; Pop two DWORD arguments and return.
  }
}
</code></pre>
<p>was verstehst du daran nicht?<br />
das rechts neben dem semikolon ist btw kein asm <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";D"
      alt="😉"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794506</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:00:07 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:06:17 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Und Ob man versucht Tabellenguckerchen zu machen umd Divisionen zu sparen.</p>
</blockquote>
<p>Wenn man <code>div()</code> benutzt, sind die Chancen eventuell höher, dass Division und Modulo gleichzeitig berechnet werden können:<br />
<a href="http://www.cplusplus.com/reference/clibrary/cstdlib/div/" rel="nofollow">http://www.cplusplus.com/reference/clibrary/cstdlib/div/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794507</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794507</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:06:17 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:35:07 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Und Ob man versucht Tabellenguckerchen zu machen umd Divisionen zu sparen.</p>
</blockquote>
<p>Wenn man <code>div()</code> benutzt, sind die Chancen eventuell höher, dass Division und Modulo gleichzeitig berechnet werden können:<br />
<a href="http://www.cplusplus.com/reference/clibrary/cstdlib/div/" rel="nofollow">http://www.cplusplus.com/reference/clibrary/cstdlib/div/</a></p>
</blockquote>
<p>Cool, das kannte ich ja noch gar nicht. Ich sollte mir mal die C Bibliothek genauer angucken, vielleicht gibt es dort ja noch mehr solche Schätze.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794530</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:35:07 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:36:48 GMT]]></title><description><![CDATA[<p>Die allerschnellste Methode ist vermutlich ca. 2^32 Zeilen lang, kostet fast gar nichts, aber der quelltext ist grob überschlagen 80GB groß <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";D"
      alt="😉"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794532</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794532</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:36:48 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:39:36 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Und Ob man versucht Tabellenguckerchen zu machen umd Divisionen zu sparen.</p>
</blockquote>
<p>Wenn man <code>div()</code> benutzt, sind die Chancen eventuell höher, dass Division und Modulo gleichzeitig berechnet werden können:<br />
<a href="http://www.cplusplus.com/reference/clibrary/cstdlib/div/" rel="nofollow">http://www.cplusplus.com/reference/clibrary/cstdlib/div/</a></p>
</blockquote>
<p>Der Compiler faßt in der Nähe liegende % und / auch selber zusammen, wenn das geht. Kein Bedarf. Außerdem macht er gar kein DIV, sondern erledigt das per Multiplikation, da der Teiler compilezeitkonstant ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794535</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:39:36 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:41:54 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>Die allerschnellste Methode ist vermutlich ca. 2^32 Zeilen lang, kostet fast gar nichts, aber der quelltext ist grob überschlagen 80GB groß</p>
</blockquote>
<p>Das ist gar nicht mal so doof. Wenn man das nur für Zahlen bis um die 1000 braucht, dann ist ein switch (den man mittels Präprozessor erstellen lässt) extrem effizient. Gibt's eigentlich ein Maximum an möglichen switch cases?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794538</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:41:54 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 22:58:10 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>Die allerschnellste Methode ist vermutlich ca. 2^32 Zeilen lang, kostet fast gar nichts, aber der quelltext ist grob überschlagen 80GB groß</p>
</blockquote>
<p>Das ist gar nicht mal so doof. Wenn man das nur für Zahlen bis um die 1000 braucht, dann ist ein switch (den man mittels Präprozessor erstellen lässt) extrem effizient. Gibt's eigentlich ein Maximum an möglichen switch cases?</p>
</blockquote>
<p>weiß ich nicht - ich habs gerad mit unsigned short versucht - mein compiler hat jz scho 7 Minuten CPU-Zeit verbraucht und noch hat sich nichts getan, außer das er ca. 32mb ram belegt hat...^^<br />
hab dir auch mal mit geschrieben, wie ich die datei erzeugt hab - damit du nicht so viel tippen musst ;o)</p>
<p>bb</p>
<pre><code class="language-cpp">#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;

void create()
{
	std::ofstream str(&quot;Q:/convert.h&quot;);
	str &lt;&lt; &quot;const char* ToString(unsigned short tmp)&quot; &lt;&lt; std::endl
		&lt;&lt; '{' &lt;&lt; std::endl
		&lt;&lt; &quot;\tswitch(tmp)&quot; &lt;&lt; std::endl
		&lt;&lt; &quot;\t{&quot; &lt;&lt; std::endl;

	for(unsigned short i(1); i != 0; ++i)
	{
		str &lt;&lt; &quot;\t\tcase &quot; &lt;&lt; i &lt;&lt; &quot;: return \&quot;&quot; &lt;&lt; i &lt;&lt; &quot;\&quot;;&quot; &lt;&lt; std::endl;
		if(i%1000 == 0)
			std::cout &lt;&lt; i &lt;&lt; &quot; / &quot; &lt;&lt; std::numeric_limits&lt;unsigned short&gt;::max() &lt;&lt; std::endl;
	}

	str &lt;&lt; &quot;default: return \&quot;0\&quot;;&quot; &lt;&lt; std::endl
		&lt;&lt; &quot;\t}&quot; &lt;&lt; std::endl
		&lt;&lt; '}' &lt;&lt; std::endl
		&lt;&lt; std::endl;
}

#include &quot;Q:/convert.h&quot;

int main()
{
//	create();

	const char* tmp = ToString(5432);
	std::cout &lt;&lt; tmp &lt;&lt; std::endl;

	system(&quot;PAUSE&quot;);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1794548</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794548</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 22:58:10 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 23:25:57 GMT]]></title><description><![CDATA[<p>MSVC schrieb:</p>
<blockquote>
<p>1&gt;Linking...<br />
1&gt;Generating code<br />
1&gt;fatal error C1510: Cannot load language resource clui.dll.<br />
1&gt;Project : error PRJ0002 : Error result 1 returned from 'c:\Program Files\Microsoft Visual Studio 9.0\VC\bin\link.exe'.</p>
</blockquote>
<p>nachdem mein laptop zwischenzeitlic gar nicht mehr reagiert hat... xD<br />
vll kanns ja der gcc!?<br />
hab iwie das gefühl, dass der versuch bleibende schäden hinterlassen hat - fühlt sich alles gerad so extrem langsam an <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f623.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--persevering_face"
      title="&gt;.&lt;"
      alt="😣"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794555</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 18 Oct 2009 23:25:57 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Sun, 18 Oct 2009 23:56:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;cstring&gt;

int main()
{
    unsigned int a = 551, b = 551;
    char string1[11] = {0,}; 
    // C
    int i = 9;
    do 
    {
        string1[i]= a%10+'0';
        a /= 10;
        i--;
    }while(a);
    strcpy(string1, &amp;string1[i+1]);

    char string2[11] = {0,};
    // Assembler
    __asm
    {
        mov  eax, b
        mov  ebx, 10
        sub  ecx, ecx
rechnen:
        xor  edx, edx
        div  ebx
        push edx
        inc  ecx
        or   eax, eax
        jne  rechnen
um:     pop  edx
        add  dl, '0'
        mov  string2[eax], dl
        inc  eax
        dec  ecx
        jne  um
    }

    return 0;
}
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794561</guid><dc:creator><![CDATA[otto8]]></dc:creator><pubDate>Sun, 18 Oct 2009 23:56:05 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 12:04:01 GMT]]></title><description><![CDATA[<p>Hier ein Beispiel für die ultimativ schnelle Lösung. Wobei ich die Geschwindigkeit nicht verglichen habe, aber es &quot;sollte&quot; schneller sein als alles andere. Das Kompilat ist bei mir stolze 38 kb groß.</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;boost/preprocessor/repetition/repeat.hpp&gt;
#include &lt;boost/preprocessor/stringize.hpp&gt;

std::string char2string(unsigned char i)
{
  switch(i){
#define MY_SWITCHER(ret,n,dummy) case n : return BOOST_PP_STRINGIZE(n);
    BOOST_PP_REPEAT(256, MY_SWITCHER,)
#undef MY_SWITCHER
  }
}
</code></pre>
<p>Das Limit für BOOST_PP_REPEAT ist derzeit 256, weswegen ich als integralen Datentyp char gewählt habe. Es ist aber natürlich möglich durch automatisch erstellten Code das Makro auf größere Wertbereiche zu erweitern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794761</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 19 Oct 2009 12:04:01 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 12:22:44 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Das Limit für BOOST_PP_REPEAT ist derzeit 256, weswegen ich als integralen Datentyp char gewählt habe. Es ist aber natürlich möglich durch automatisch erstellten Code das Makro auf größere Wertbereiche zu erweitern.</p>
</blockquote>
<p>aber auf wie viel größere wertebereiche weist du auch nicht!?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794773</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 19 Oct 2009 12:22:44 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 12:36:06 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Das Limit für BOOST_PP_REPEAT ist derzeit 256, weswegen ich als integralen Datentyp char gewählt habe. Es ist aber natürlich möglich durch automatisch erstellten Code das Makro auf größere Wertbereiche zu erweitern.</p>
</blockquote>
<p>aber auf wie viel größere wertebereiche weist du auch nicht!?</p>
<p>bb</p>
</blockquote>
<p>Das sollte beliebig sein. Es ist eine reine Fleißaufgabe, Makrodefinitionen in dieser Art zu erstellen:</p>
<pre><code class="language-cpp"># define BOOST_PP_REPEAT_1_0(m, d)
# define BOOST_PP_REPEAT_1_1(m, d) m(2, 0, d)
# define BOOST_PP_REPEAT_1_2(m, d) BOOST_PP_REPEAT_1_1(m, d) m(2, 1, d)
# define BOOST_PP_REPEAT_1_3(m, d) BOOST_PP_REPEAT_1_2(m, d) m(2, 2, d)
# define BOOST_PP_REPEAT_1_4(m, d) BOOST_PP_REPEAT_1_3(m, d) m(2, 3, d)
# define BOOST_PP_REPEAT_1_5(m, d) BOOST_PP_REPEAT_1_4(m, d) m(2, 4, d)
# define BOOST_PP_REPEAT_1_6(m, d) BOOST_PP_REPEAT_1_5(m, d) m(2, 5, d)
// und so weiter
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1794784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794784</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 19 Oct 2009 12:36:06 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 12:46:43 GMT]]></title><description><![CDATA[<p>sry, ich hab natürlich nicht vom makro geredet sondern vom switch^^<br />
der msvc wollt mir zumindest hier keine fkt für unsigned short compilieren -.-<br />
das heißt, 2^16(mit default) sind wohl schon zu viel^^<br />
wenn ich ma wieder bissl lange weile hab, guck ich mal, wie viel er schafft^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794794</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794794</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 19 Oct 2009 12:46:43 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 13:47:07 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>sry, ich hab natürlich nicht vom makro geredet sondern vom switch^^<br />
der msvc wollt mir zumindest hier keine fkt für unsigned short compilieren -.-<br />
das heißt, 2^16(mit default) sind wohl schon zu viel^^<br />
wenn ich ma wieder bissl lange weile hab, guck ich mal, wie viel er schafft^^</p>
<p>bb</p>
</blockquote>
<p>Hast du im Debug- oder Releasebuild kompiliert?<br />
Der gcc kompiliert das ohne Optimierungen recht flott, im Releasebuild sitzt er aber ewig lange dran.<br />
Übrigens, sollte immer mindestens genauso performant wie switch sein und auch mit angeschalteten Optimierungen deutlich schneller kompilierbar sein:</p>
<pre><code class="language-cpp">const char* ToString(unsigned short val)
{
  static const char* strings[65536]={&quot;0&quot;,&quot;1&quot;, ... ,&quot;65535&quot;};
  return strings[val]; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1794828</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794828</guid><dc:creator><![CDATA[Nanyuki]]></dc:creator><pubDate>Mon, 19 Oct 2009 13:47:07 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 14:15:32 GMT]]></title><description><![CDATA[<p>Nanyuki schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>sry, ich hab natürlich nicht vom makro geredet sondern vom switch^^<br />
der msvc wollt mir zumindest hier keine fkt für unsigned short compilieren -.-<br />
das heißt, 2^16(mit default) sind wohl schon zu viel^^<br />
wenn ich ma wieder bissl lange weile hab, guck ich mal, wie viel er schafft^^</p>
<p>bb</p>
</blockquote>
<p>Hast du im Debug- oder Releasebuild kompiliert?<br />
Der gcc kompiliert das ohne Optimierungen recht flott, im Releasebuild sitzt er aber ewig lange dran.<br />
Übrigens, sollte immer mindestens genauso performant wie switch sein und auch mit angeschalteten Optimierungen deutlich schneller kompilierbar sein:</p>
<pre><code class="language-cpp">const char* ToString(unsigned short val)
{
  static const char* strings[65536]={&quot;0&quot;,&quot;1&quot;, ... ,&quot;65535&quot;};
  return strings[val]; 
}
</code></pre>
</blockquote>
<p>release^^<br />
hmm - ok, das wäre vermutlich wirklich sehr viel klüger <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";D"
      alt="😉"
    /><br />
ich habs gerad mal versucht:</p>
<p>MSVC schrieb:</p>
<blockquote>
<p>fatal error C1128: number of sections exceeded object file format limit : compile with /bigobj</p>
</blockquote>
<p>gesagt, getan...<br />
dann ging es aber ohne probleme...<br />
die exe ist eben nur 1MB groß, obwohl sie so ziemlich gar nix macht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";D"
      alt="😉"
    /></p>
<pre><code class="language-cpp">//#define MY_BUILD	1
#define MY_PATH		&quot;Q:/convert.h&quot;

#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;

void create()
{
#if defined(MY_BUILD)
	std::ofstream str(MY_PATH);
	str &lt;&lt; &quot;const char* ToString(unsigned short nr)&quot; &lt;&lt; std::endl
		&lt;&lt; '{' &lt;&lt; std::endl
		&lt;&lt; &quot;\tstatic const char* strings[] = {\&quot;0\&quot;&quot; &lt;&lt; std::endl &lt;&lt; &quot;\t\t&quot;;

	for(unsigned short i(1); i != 0; ++i)
	{
		str &lt;&lt; &quot;, \&quot;&quot; &lt;&lt; i &lt;&lt; &quot;\&quot;&quot;;
		if(i%10 == 0)
			str &lt;&lt; std::endl &lt;&lt; &quot;\t\t&quot;;
		if(i%1000 == 0)
			std::cout &lt;&lt; i &lt;&lt; &quot; / &quot; &lt;&lt; std::numeric_limits&lt;unsigned short&gt;::max() &lt;&lt; std::endl;
	}

	str &lt;&lt; std::endl
		&lt;&lt; &quot;\t};&quot; &lt;&lt; std::endl
		&lt;&lt; &quot;\treturn strings[nr];&quot; &lt;&lt; std::endl
		&lt;&lt; '}' &lt;&lt; std::endl
		&lt;&lt; std::endl;
#endif
}

#include &quot;Console/console.hpp&quot;
#if !defined(MY_BUILD)
#	include MY_PATH
#endif

void test()
{
#if !defined(MY_BUILD)
	const char* tmp = ToString(5432);
	std::cout &lt;&lt; tmp &lt;&lt; std::endl;
#endif
}

int main()
{
	create();

	test();

	my::console::wait();
}
</code></pre>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794858</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794858</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 19 Oct 2009 14:15:32 GMT</pubDate></item><item><title><![CDATA[Reply to Effektivster Weg, unsigned int in string umzuwandeln on Mon, 19 Oct 2009 18:27:00 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Der Compiler faßt in der Nähe liegende % und / auch selber zusammen, wenn das geht.</p>
</blockquote>
<p>Habe ich auch gedacht, aber da das eine im Schleifenrumpf und das andere in der <code>do while</code> -Bedingung liegt... Vielleicht bin ich allerdings etwas zu vorsichtig, was solche Dinge anbelangt.</p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Cool, das kannte ich ja noch gar nicht. Ich sollte mir mal die C Bibliothek genauer angucken, vielleicht gibt es dort ja noch mehr solche Schätze.</p>
</blockquote>
<p>Ja, es gibt einige Dinge in der Standardbibliothek, die man fast nie braucht und kaum mitbekommt. Hast du schon mal von <code>sig_atomic_t</code> ( <code>&lt;csignal&gt;</code> ), <code>inner_product</code> ( <code>&lt;numeric&gt;</code> ) oder <code>indirect_array</code> ( <code>&lt;valarray&gt;</code> ) gehört? Ich stöbere manchmal auf <a href="http://www.cplusplus.com" rel="nofollow">www.cplusplus.com</a>, das ist zum Teil recht interessant.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1794992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1794992</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 19 Oct 2009 18:27:00 GMT</pubDate></item></channel></rss>