<?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[Warum unsigned langsamer als signed?]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes konstruiertes Beispiel:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctime&gt;
using namespace std;

int main()
{
    double time1,time2;
    int i,a,b,c;
    a=2;
    b=5;

    time1 = clock();
    for(i=1;i&lt;90000000;++i)
        c=(a*i)/(b*i);
    time2 = clock();

    cout &lt;&lt; c &lt;&lt; &quot;, &quot; &lt;&lt; ((double)(time2-time1)/CLOCKS_PER_SEC)*1000 &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>Die for-Schleife ist nur dazu da, um eine messbare Zeit zu bekommen.<br />
Mit int (für i,a,b,c) erhalte ich 305 ms im Durschnitt, mit unsigned int 375 ms.</p>
<p>Warum gibt es hier Performance-Unterschiede? Ich dachte, dass nur der Wertebereich sich ändert.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/297215/warum-unsigned-langsamer-als-signed</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 14:32:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/297215.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 Dec 2011 12:52:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 12:52:33 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes konstruiertes Beispiel:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctime&gt;
using namespace std;

int main()
{
    double time1,time2;
    int i,a,b,c;
    a=2;
    b=5;

    time1 = clock();
    for(i=1;i&lt;90000000;++i)
        c=(a*i)/(b*i);
    time2 = clock();

    cout &lt;&lt; c &lt;&lt; &quot;, &quot; &lt;&lt; ((double)(time2-time1)/CLOCKS_PER_SEC)*1000 &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>Die for-Schleife ist nur dazu da, um eine messbare Zeit zu bekommen.<br />
Mit int (für i,a,b,c) erhalte ich 305 ms im Durschnitt, mit unsigned int 375 ms.</p>
<p>Warum gibt es hier Performance-Unterschiede? Ich dachte, dass nur der Wertebereich sich ändert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159686</guid><dc:creator><![CDATA[*Cassiopeia*]]></dc:creator><pubDate>Wed, 21 Dec 2011 12:52:33 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:01:23 GMT]]></title><description><![CDATA[<p>Das messe ich auch.</p>
<p>Mit unsigned int:</p>
<pre><code class="language-asm">.L2:
	movl	%ecx, %eax
	xorl	%edx, %edx
	addl	$2, %ecx
	divl	%ebx
	addl	$5, %ebx
	cmpl	$180000000, %ecx
	movl	%eax, %r12d
	jne	.L2
</code></pre>
<p>Mit signed int:</p>
<pre><code class="language-asm">.L2:
	movl	%ecx, %edx
	movl	%ecx, %eax
	addl	$2, %ecx
	sarl	$31, %edx
	idivl	%ebx
	addl	$5, %ebx
	cmpl	$180000000, %ecx
	movl	%eax, %r12d
	jne	.L2
</code></pre>
<p>Komisch.</p>
<p>Übrigens, wenn die Schleife zehnmal so lang geht, habe ich 30ms zugunsten von unsigned int. 3230 zu 3200.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159688</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159688</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:01:23 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:13:24 GMT]]></title><description><![CDATA[<p>div und idiv müssen nicht gleich schnell sein. Kommt drauf an wie es implementiert ist.<br />
Ist aber anscheinend nicht unüblich, dass sie unterschiedlich sind:<br />
<a href="http://faydoc.tripod.com/cpu/idiv.htm" rel="nofollow">http://faydoc.tripod.com/cpu/idiv.htm</a><br />
vs.<br />
<a href="http://faydoc.tripod.com/cpu/div.htm" rel="nofollow">http://faydoc.tripod.com/cpu/div.htm</a></p>
<p>div (reg,32): 41 clocks<br />
idiv (reg,32): 46 clocks</p>
<p>unsigned müsste also schneller sein als signed.<br />
Könnte sein, dass die erste Messung doch zu viele Fehler hat. Ich würde das Ergebnis von volkard erwarten (die 10er Version).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159694</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159694</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:13:24 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:25:37 GMT]]></title><description><![CDATA[<p>Ich kann die Ergebnisse nicht nachvollziehen. Getestet auf i7, GCC 4.4, O3 Optimierung, testweise auch mal mit march=&quot;native&quot; (hat sogar ein bisschen was gebracht). Bei gleicher Optimierung exakt die gleiche Zeit.</p>
<p>Intelcompiler 11 optimiert die Schleife gänzlich weg, Laufzeit 0. Habe aber gerade keine Lust, ihm das Optimieren künstlich zu erschweren, so dass er das nicht mehr macht.</p>
<p>edit: Ach ja, der Code des GCC unsigned:</p>
<pre><code class="language-asm">.L4:
	movl	%ecx, %eax
	xorl	%edx, %edx
	addl	$2, %ecx
	divl	%ebx
	addl	$5, %ebx
	cmpl	$1800000000, %ecx
	movl	%eax, %r13d
	jne	.L4
</code></pre>
<p>Mit march=native (man beachte den kleinen aber feinen Unterschied:</p>
<pre><code class="language-asm">.L4:
	movl	%ecx, %eax
	xorl	%edx, %edx
	addl	$2, %ecx
	divl	%ebx
	addl	$5, %ebx
	movl	%eax, %r13d
	cmpl	$1800000000, %ecx
	jne	.L4
</code></pre>
<p>Und mit int (hier nur die normale Version, die native Version könnt ihr euch denken):</p>
<pre><code class="language-asm">.L4:
	movl	%ecx, %edx
	movl	%ecx, %eax
	addl	$2, %ecx
	sarl	$31, %edx
	idivl	%ebx
	addl	$5, %ebx
	cmpl	$1800000000, %ecx
	movl	%eax, %r13d
	jne	.L4
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2159696</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159696</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:25:37 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:32:21 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Ich kann die Ergebnisse nicht nachvollziehen. Getestet auf i7, GCC 4.4, O3 Optimierung, testweise auch mal mit march=&quot;native&quot; (hat sogar ein bisschen was gebracht). Überall exakt das gleiche.</p>
</blockquote>
<p>Na, dann messe ich auch mal ausführlicher (i7, gcc4.5, -O3 -march=native):</p>
<pre><code>g++ -O3 -march=native main.cpp &amp;&amp; (for i in {1..100}; do time ./a.out ; done)2&gt;&amp;1 | grep real | sort | head
</code></pre>
<p>unsigned:</p>
<pre><code>real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
real	0m0.320s
</code></pre>
<p>signed:</p>
<pre><code>real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
real	0m0.293s
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2159703</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159703</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:32:21 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:44:55 GMT]]></title><description><![CDATA[<p>Ich hatte es im Ausgangspost ohne Optimierung getestet, aber habs jetzt nochmal mit -03 und -march=native gemacht (ebenfalls i7 und gcc 4.6.1). Ich komme auch auf die Zeiten von volkard (270 vs. 312 für signed). Auch wenn ich die Schleife länger laufen lasse, bleibt signed bei mir schneller (2920 vs. 3120)</p>
<p>Mit dem Assemblercode kenne ich mich leider nicht wirklich aus... ich hatte mich bei einem größeren Projekt nur gewundert, dass mein Code mit unsigned langsamer läuft und wollte das deswegen etwas genauer angucken mit dem kleinen Beispiel. Und zumindest auf meinem Rechner hat sich das ja bestätigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159707</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159707</guid><dc:creator><![CDATA[*Cassiopeia*]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:44:55 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:53:09 GMT]]></title><description><![CDATA[<p>Leider kann man es nicht verallgemeinern. Bei mir ist meistens unsigned ein wenig schneller.</p>
<p>Vielleicht gibt es bei signed einen Schaltkreis mehr, der vorher einen Größenvergleich macht und als Nebenprodukt eine schnelle Nullnummer machen kann, falls der Zähler kleiner als der Nenner ist. Das habe ich mal weggemacht.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;ctime&gt;
using namespace std;

int main()
{
    cout&lt;&lt;&quot;test\n&quot;;
    double time1,time2;
    signed int i,a,b,c;
    c=0;
    a=2;
    b=5;

    time1 = clock();
    for(i=1;i&lt;90000000;++i)
        c+=(b*i)/(a*i);//andersrum als vorher, und += gegen Wegoptimierung
    time2 = clock();

    cout &lt;&lt; c &lt;&lt; &quot;, &quot; &lt;&lt; ((double)(time2-time1)/CLOCKS_PER_SEC)*1000 &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>unsigned: 373<br />
signed: 373</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159710</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:53:09 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 13:58:08 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a>: Das ist ja mal lustig:</p>
<p>signed:</p>
<pre><code>real 0.339	user 0.340	sys 0.000	pcpu 100.22
real 0.339	user 0.340	sys 0.000	pcpu 100.27
real 0.339	user 0.340	sys 0.000	pcpu 100.28
real 0.339	user 0.340	sys 0.000	pcpu 100.29
real 0.339	user 0.340	sys 0.000	pcpu 100.29
real 0.339	user 0.340	sys 0.000	pcpu 100.30
real 0.339	user 0.340	sys 0.000	pcpu 100.31
real 0.339	user 0.340	sys 0.000	pcpu 100.31
real 0.339	user 0.340	sys 0.000	pcpu 100.31
real 0.339	user 0.340	sys 0.000	pcpu 100.32
</code></pre>
<p>unsigned:</p>
<pre><code>real 0.339	user 0.320	sys 0.020	pcpu 100.29
real 0.339	user 0.330	sys 0.010	pcpu 100.28
real 0.339	user 0.330	sys 0.010	pcpu 100.33
real 0.339	user 0.340	sys 0.000	pcpu 100.15
real 0.339	user 0.340	sys 0.000	pcpu 100.16
real 0.339	user 0.340	sys 0.000	pcpu 100.17
real 0.339	user 0.340	sys 0.000	pcpu 100.17
real 0.339	user 0.340	sys 0.000	pcpu 100.20
real 0.339	user 0.340	sys 0.000	pcpu 100.21
real 0.339	user 0.340	sys 0.000	pcpu 100.22
</code></pre>
<p>Den Assembleroutput haben wir ja schon verlgichen, die Compilerversion kann's nicht sein. Das Programm ist gewiss auch nicht durch die externen Komponenten gebremst, das sollte reine CPU sein. Hat vielleicht Intel da ganz was grundlegendes an seiner Architektur geändert? Ich habe hier einen i7-920, also ein Nehalem. Da deiner ein bisschen schneller ist und ich weiß, dass du dir vor ein paar Monaten einen neuen Rechner gekauft hast, nehme ich mal an, du hast eine der neueren Mikroarchitekturen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159714</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159714</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 21 Dec 2011 13:58:08 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 14:00:09 GMT]]></title><description><![CDATA[<p>Glauben manche Leute eigentlich, dass ihr Code so perfekt ist, dass sie auf sowas achten müssen? Oder ist euch nur langweilig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159715</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159715</guid><dc:creator><![CDATA[$geegt$]]></dc:creator><pubDate>Wed, 21 Dec 2011 14:00:09 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 14:01:51 GMT]]></title><description><![CDATA[<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>g</mi><mi>e</mi><mi>e</mi><mi>g</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">geegt</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.61508em;"></span><span class="strut bottom" style="height:0.80952em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">e</span><span class="mord mathit">e</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mord mathit">t</span></span></span></span> schrieb:</p>
<blockquote>
<p>Glauben manche Leute eigentlich, dass ihr Code so perfekt ist, dass sie auf sowas achten müssen? Oder ist euch nur langweilig?</p>
</blockquote>
<p>Interesse und auch Langeweile, ja ;)... natürlich sind das alles keine Größenordnungen, aber interessieren tuts mich schon.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159718</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159718</guid><dc:creator><![CDATA[*Cassiopeia*]]></dc:creator><pubDate>Wed, 21 Dec 2011 14:01:51 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 14:14:11 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Ich habe hier einen i7-920, also ein Nehalem. Da deiner ein bisschen schneller ist und ich weiß, dass du dir vor ein paar Monaten einen neuen Rechner gekauft hast, nehme ich mal an, du hast eine der neueren Mikroarchitekturen?</p>
</blockquote>
<p>Ja, &quot;Sandy Bridge&quot; hat mich erst überzeugt.<br />
<a href="http://geizhals.at/deutschland/580311" rel="nofollow">http://geizhals.at/deutschland/580311</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159725</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 21 Dec 2011 14:14:11 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 14:55:33 GMT]]></title><description><![CDATA[<p>Ich habe aus Interesse den Code auch mal kompiliert mit (i&lt;100000000) in einer Windows 7 VM und eine Feststellung gemacht. Das Resultat mit optimierten Solution Settings: Mit int 375, unsigned int 450!</p>
<p>Und dann habe ich folgendes gemacht: Visual C++ mit x64 Target und maximalen Optimierungen. Resultat: Beides 125. Kann es sein, dass der Compiler für x86 nicht mehr gut optimiert? Oder dass die neuen CPUs nicht mehr so toll mit den normalen ints umgehen?</p>
<p>CPU: Sandy Bridge i7 QM, 2.2 GHz.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159753</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159753</guid><dc:creator><![CDATA[*rant*]]></dc:creator><pubDate>Wed, 21 Dec 2011 14:55:33 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 15:11:35 GMT]]></title><description><![CDATA[<p>/rant/ schrieb:</p>
<blockquote>
<p>Oder dass die neuen CPUs nicht mehr so toll mit den normalen ints umgehen?</p>
</blockquote>
<p>Nette Idee. Habe mit (un)signed long long gemessen.<br />
unsigned long long: 0.750s<br />
signed long long: 0.784s</p>
<p>Jetzt ist die Welt wieder in Ordnung. :xmas2: :xmas1:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159758</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 21 Dec 2011 15:11:35 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Wed, 21 Dec 2011 16:34:07 GMT]]></title><description><![CDATA[<p>mit einem Atom N330 bekomme ich diese Wert (unter 64-bit-Gentoo):<br />
unsigned: 3000 ms<br />
signed: 3740 ms<br />
unsigned long long: 7360 ms<br />
signed long long: 8730 ms</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2159799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2159799</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 21 Dec 2011 16:34:07 GMT</pubDate></item><item><title><![CDATA[Reply to Warum unsigned langsamer als signed? on Thu, 22 Dec 2011 00:31:17 GMT]]></title><description><![CDATA[<p>Was zu dem gefunden:<br />
<a href="http://stackoverflow.com/questions/410982/is-there-a-difference-in-term-of-performance-between-unsigned-int-and-int-on" rel="nofollow">http://stackoverflow.com/questions/410982/is-there-a-difference-in-term-of-performance-between-unsigned-int-and-int-on</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2160004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2160004</guid><dc:creator><![CDATA[user02]]></dc:creator><pubDate>Thu, 22 Dec 2011 00:31:17 GMT</pubDate></item></channel></rss>