<?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[Variable in Schleife anlegen schlecht?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ist es schlecht, wenn ich eine Variable in der schleife anlege?</p>
<pre><code class="language-cpp">for(int i=0; i&lt;10; i++) {
float foo = ...
...
}
</code></pre>
<p>Wird dann bei jedem Durchlauf eine neue Variable auf dem Stack gelegt (und die alte gelöscht)?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/204966/variable-in-schleife-anlegen-schlecht</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 23:45:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/204966.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 08 Feb 2008 19:04:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 19:04:32 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ist es schlecht, wenn ich eine Variable in der schleife anlege?</p>
<pre><code class="language-cpp">for(int i=0; i&lt;10; i++) {
float foo = ...
...
}
</code></pre>
<p>Wird dann bei jedem Durchlauf eine neue Variable auf dem Stack gelegt (und die alte gelöscht)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452184</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452184</guid><dc:creator><![CDATA[Performancefrage]]></dc:creator><pubDate>Fri, 08 Feb 2008 19:04:32 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 19:06:34 GMT]]></title><description><![CDATA[<p>erstens, ja wird natürlich immer wieder angelegt<br />
zweitens: wozu brauchst du das? Wenn du ein problem hast wo du denkst du brauchst es können wir dir sicher besser helfen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452187</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452187</guid><dc:creator><![CDATA[XaTrIxX]]></dc:creator><pubDate>Fri, 08 Feb 2008 19:06:34 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 19:18:49 GMT]]></title><description><![CDATA[<p>Ich würde mal sagen, der Compiler muss sicherstellen, dass sich die Variable so verhält als würde sie jedes mal neu erstellt.</p>
<p>Da der Wert eines Floats aber vor seiner initialisierung undefiniert ist kann es IMO gut sein, dass der alte Wert da weiterhin drinnesteht und nichts neu angelegt wurde.</p>
<p>Btw: Scott Meyers empfielht in »Effective C++« variablen so spät als möglich anzulegen und explizit auch, Variablen erst im Rumpf einer Schleife anzulegen, wenn diese nur dort benötigt werden <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="😉"
    /> =&gt; ich würde mal davon ausgehen dass du keinen Leistungsunterschied merkst.</p>
<p>EDIT://</p>
<pre><code class="language-cpp">int main()
{
        /*float f;*/
	for (unsigned a(0); a &lt; 10; a++)
	{
		/*float */f = 12;
	}
}
</code></pre>
<p>Außen:</p>
<pre><code class="language-asm">.file	&quot;loop.cpp&quot;
	.text
	.align 2
.globl main
	.type	main, @function
main:
.LFB2:
	leal	4(%esp), %ecx
.LCFI0:
	andl	$-16, %esp
	pushl	-4(%ecx)
.LCFI1:
	pushl	%ebp
.LCFI2:
	movl	%esp, %ebp
.LCFI3:
	pushl	%ecx
.LCFI4:
	subl	$16, %esp
.LCFI5:
	movl	$0, -8(%ebp)
	jmp	.L2
.L3:
	movl	$0x41400000, %eax
	movl	%eax, -12(%ebp)
	addl	$1, -8(%ebp)
.L2:
	cmpl	$9, -8(%ebp)
	jbe	.L3
	movl	$0, %eax
	addl	$16, %esp
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
.LFE2:
	.size	main, .-main
.globl __gxx_personality_v0
	.ident	&quot;GCC: (GNU) 4.2.3 20080114 (prerelease) (Debian 4.2.2-7)&quot;
	.section	.note.GNU-stack,&quot;&quot;,@progbits
</code></pre>
<p>Innen</p>
<pre><code class="language-asm">.file	&quot;loop.cpp&quot;
	.text
	.align 2
.globl main
	.type	main, @function
main:
.LFB2:
	leal	4(%esp), %ecx
.LCFI0:
	andl	$-16, %esp
	pushl	-4(%ecx)
.LCFI1:
	pushl	%ebp
.LCFI2:
	movl	%esp, %ebp
.LCFI3:
	pushl	%ecx
.LCFI4:
	subl	$16, %esp
.LCFI5:
	movl	$0, -12(%ebp)
	jmp	.L2
.L3:
	movl	$0x41400000, %eax
	movl	%eax, -8(%ebp)
	addl	$1, -12(%ebp)
.L2:
	cmpl	$9, -12(%ebp)
	jbe	.L3
	movl	$0, %eax
	addl	$16, %esp
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
.LFE2:
	.size	main, .-main
.globl __gxx_personality_v0
	.ident	&quot;GCC: (GNU) 4.2.3 20080114 (prerelease) (Debian 4.2.2-7)&quot;
	.section	.note.GNU-stack,&quot;&quot;,@progbits
</code></pre>
<p>Unterschied</p>
<pre><code class="language-asm">--- aussen.S	2008-02-08 20:14:07.000000000 +0100
+++ innen.S	2008-02-08 20:14:23.000000000 +0100
@@ -18,14 +18,14 @@
 .LCFI4:
 	subl	$16, %esp
 .LCFI5:
-	movl	$0, -8(%ebp)
+	movl	$0, -12(%ebp)
 	jmp	.L2
 .L3:
 	movl	$0x41400000, %eax
-	movl	%eax, -12(%ebp)
-	addl	$1, -8(%ebp)
+	movl	%eax, -8(%ebp)
+	addl	$1, -12(%ebp)
 .L2:
-	cmpl	$9, -8(%ebp)
+	cmpl	$9, -12(%ebp)
 	jbe	.L3
 	movl	$0, %eax
 	addl	$16, %esp
</code></pre>
<p>GNU GCC 4.2.3-pre (Debian Lenny)</p>
<p>EDIT://<br />
Ganz ohne Optimierung und für mich sieht das nach keinerlei Performance-Unterschieden aus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452192</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452192</guid><dc:creator><![CDATA[darthdespotism]]></dc:creator><pubDate>Fri, 08 Feb 2008 19:18:49 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 19:41:58 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10469">@XaTrIxX</a>: So natürlich ist das überhaupt nicht, dass die jedes Mal neu angelegt wird.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/15276">@darthdespotism</a>: Das hätte ich nicht gedacht, dass es sogar empfohlen wird Variablen im Schleifenrumpf anzulegen.<br />
Wenn ich jetzt noch Assembler könnte, hätte ich bestimmt einen noch tieferen Durchblick. <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/1452212</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452212</guid><dc:creator><![CDATA[Performancefrage]]></dc:creator><pubDate>Fri, 08 Feb 2008 19:41:58 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 20:18:40 GMT]]></title><description><![CDATA[<p>Das Diff sagt aus: Die Codes brauchen exakt die gleiche Anzahl CPU-Takte <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/1452236</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452236</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 08 Feb 2008 20:18:40 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 20:23:38 GMT]]></title><description><![CDATA[<p>darthdespotism schrieb:</p>
<blockquote>
<p>Ganz ohne Optimierung und für mich sieht das nach keinerlei Performance-Unterschieden aus</p>
</blockquote>
<p>Und was hat das mit der Frage zu tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452241</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452241</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 08 Feb 2008 20:23:38 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 20:40:09 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>darthdespotism schrieb:</p>
<blockquote>
<p>Ganz ohne Optimierung und für mich sieht das nach keinerlei Performance-Unterschieden aus</p>
</blockquote>
<p>Und was hat das mit der Frage zu tun?</p>
</blockquote>
<p>Der OP hiess &quot;Performancefrage&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452255</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 08 Feb 2008 20:40:09 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 21:12:46 GMT]]></title><description><![CDATA[<p>LordJaxom schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>darthdespotism schrieb:</p>
<blockquote>
<p>Ganz ohne Optimierung und für mich sieht das nach keinerlei Performance-Unterschieden aus</p>
</blockquote>
<p>Und was hat das mit der Frage zu tun?</p>
</blockquote>
<p>Der OP hiess &quot;Performancefrage&quot;</p>
</blockquote>
<p>Eben. Was hat &quot;nicht optimiert&quot; eines <em>inhaltslosen Programmes</em> mit &quot;Performance&quot; im <em>Allgemeinen</em> zu tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452276</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452276</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 08 Feb 2008 21:12:46 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 21:19:29 GMT]]></title><description><![CDATA[<p>Die eingebauten Typen machen keinen Geschwindigkeitsunterschied.</p>
<p>Nur wenn nicht-eingebaute Typen brauchst, macht es einen Unterschied.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452278</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 08 Feb 2008 21:19:29 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 21:35:42 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Eben. Was hat &quot;nicht optimiert&quot; eines <em>inhaltslosen Programmes</em> mit &quot;Performance&quot; im <em>Allgemeinen</em> zu tun?</p>
</blockquote>
<p>Es zeigt in diesem Fall, dass die Variable nicht &quot;immer wieder auf dem Stack gelegt und dann wieder gelöscht&quot; wird - wie es im OP gefragt wird. Das wird in inhaltsvollen Programmen und mit anderen integralen Typen nicht anders sein.</p>
<p>drakon schrieb:</p>
<blockquote>
<p>Nur wenn nicht-eingebaute Typen brauchst, macht es einen Unterschied.</p>
</blockquote>
<p>Klar, weil das Objekt jedesmal neu konstruiert wird, selbst wenn es nicht neu allokiert wird (auf dem Stack, s.o.). Da sollte man dann abwägen ob es besser ist das Objekt ausserhalb der Schleife zu definieren und innerhalb wiederzuverwenden. Aber selbst hier kann man IMHO nicht pauschal sagen, dass eine Wiederverwendung (etwa durch ein clear()) besser wäre als eine Neukonstruktion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452294</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 08 Feb 2008 21:35:42 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in Schleife anlegen schlecht? on Fri, 08 Feb 2008 21:39:17 GMT]]></title><description><![CDATA[<p>LordJaxom schrieb:</p>
<blockquote>
<p>Aber selbst hier kann man IMHO nicht pauschal sagen, dass eine Wiederverwendung (etwa durch ein clear()) besser wäre als eine Neukonstruktion.</p>
</blockquote>
<p>Ja, das könnte sein. Weiss nicht genau. Das müsste man fast einmal testen. Denke kommt auch noch die Optimierung des Compilers an, aber ein eigenes Objekt wird man so in einer Schlaufe ja selten brauchen... Auf jedenfall habe ich das noch nie entdeckt..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1452298</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1452298</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 08 Feb 2008 21:39:17 GMT</pubDate></item></channel></rss>