<?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 wird der Code nicht optimiert?]]></title><description><![CDATA[<p>Wieso ist CodeA langsamer als CodeB?</p>
<p>CodeA:</p>
<pre><code>string s = &quot;Hello World!&quot;;
int length = s.length();

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; length; ++j)
	{
		sum += length;
	}
}
</code></pre>
<p>CodeB:</p>
<pre><code>string s = &quot;Hello World!&quot;;

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; s.length(); ++j)
	{
		sum += s.length();
	}
}
</code></pre>
<p>CodeA braucht 11540ms.<br />
CodeB braucht 7090ms.</p>
<p>Müsste CodeB nicht langsamer sein, wegen den vielen length() Aufrufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/317589/warum-wird-der-code-nicht-optimiert</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Jul 2026 14:21:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/317589.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Jun 2013 18:09:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:09:32 GMT]]></title><description><![CDATA[<p>Wieso ist CodeA langsamer als CodeB?</p>
<p>CodeA:</p>
<pre><code>string s = &quot;Hello World!&quot;;
int length = s.length();

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; length; ++j)
	{
		sum += length;
	}
}
</code></pre>
<p>CodeB:</p>
<pre><code>string s = &quot;Hello World!&quot;;

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; s.length(); ++j)
	{
		sum += s.length();
	}
}
</code></pre>
<p>CodeA braucht 11540ms.<br />
CodeB braucht 7090ms.</p>
<p>Müsste CodeB nicht langsamer sein, wegen den vielen length() Aufrufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330886</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:09:32 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:24:01 GMT]]></title><description><![CDATA[<p>Das ist die falsche Frage bzw. die falsche Art, deine Frage zu stellen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330891</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330891</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:24:01 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:32:04 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>CodeA braucht 11540ms.<br />
CodeB braucht 7090ms.</p>
</blockquote>
<p>Mit -O3 bekomme ich ein ähnliches Ergebnis (CodeA ein ganz klein wenig langsamer bis gleich schnell).<br />
Mit -O2 bekomme ich dasselbe.<br />
Mit -O1 bekomme ich raus, dass CodeA schneller ist.</p>
<blockquote>
<p>Müsste CodeB nicht langsamer sein, wegen den vielen length() Aufrufen?</p>
</blockquote>
<p>Nö, wird eh geinlined. Hängt aber auch von der Implementierung ab - <code>std::string</code> könnte auch zwei Zeiger speichern...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330892</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:32:04 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:37:58 GMT]]></title><description><![CDATA[<p>Zwei Zeiger auf was?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330896</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330896</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:37:58 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:41:02 GMT]]></title><description><![CDATA[<p>Auf Stringanfang und -ende.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330897</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330897</guid><dc:creator><![CDATA[SG1]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:41:02 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:54:49 GMT]]></title><description><![CDATA[<p>Ok danke,<br />
aber das mit den zwei Zeigern versteh ich irgendwie noch nicht,<br />
was soll das bringen, bzw. warum sollte das schneller als eine Addition mit __int64 sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330898</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:54:49 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 18:55:06 GMT]]></title><description><![CDATA[<p>Also das ist mal der Assembler den mir GCC 4.9 generiert.</p>
<pre><code>.file	&quot;TEMP2.cxx&quot;
	.section	.rodata.str1.1,&quot;aMS&quot;,@progbits,1
.LC0:
	.string	&quot;Hello World!&quot;
.LC1:
	.string	&quot;sum: &quot;
	.text
	.globl	_Z3foov
	.type	_Z3foov, @function
_Z3foov:
.LFB1219:
	.cfi_startproc
	.cfi_personality 0x3,__gxx_personality_v0
	.cfi_lsda 0x3,.LLSDA1219
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	leaq	29(%rsp), %rdx
	movl	$.LC0, %esi
	leaq	16(%rsp), %rdi
.LEHB0:
	call	_ZNSsC1EPKcRKSaIcE
.LEHE0:
	movq	16(%rsp), %rax
	movl	-24(%rax), %edx
	movl	$1000000000, %esi
	movl	$0, %ebx
	movslq	%edx, %rcx
	jmp	.L2
.L6:
	addq	%rcx, %rbx
	addl	$1, %eax
	cmpl	%eax, %edx
	jg	.L6
.L5:
	subl	$1, %esi
	je	.L4
.L2:
	testl	%edx, %edx
	jle	.L5
	movl	$0, %eax
	jmp	.L6
.L4:
	movl	$5, %edx
	movl	$.LC1, %esi
	movl	$_ZSt4cout, %edi
.LEHB1:
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	%rbx, %rsi
	movl	$_ZSt4cout, %edi
	call	_ZNSo9_M_insertImEERSoT_
	movb	$10, 15(%rsp)
	movl	$1, %edx
	leaq	15(%rsp), %rsi
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.LEHE1:
	movq	16(%rsp), %rax
	leaq	-24(%rax), %rdi
	cmpq	$_ZNSs4_Rep20_S_empty_rep_storageE, %rdi
	je	.L1
	leaq	-8(%rax), %rdx
	movl	$_ZL28__gthrw___pthread_key_createPjPFvPvE, %ecx
	testq	%rcx, %rcx
	je	.L8
	movl	$-1, %eax
	lock xaddl	%eax, (%rdx)
	jmp	.L9
.L8:
	movl	-8(%rax), %edx
	leal	-1(%rdx), %ecx
	movl	%ecx, -8(%rax)
	movl	%edx, %eax
.L9:
	testl	%eax, %eax
	jg	.L1
	leaq	30(%rsp), %rsi
	call	_ZNSs4_Rep10_M_destroyERKSaIcE
	jmp	.L1
.L11:
	movq	%rax, %rbx
	movq	16(%rsp), %rax
	leaq	-24(%rax), %rdi
	leaq	31(%rsp), %rsi
	call	_ZNSs4_Rep10_M_disposeERKSaIcE
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume
.LEHE2:
.L1:
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1219:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,&quot;a&quot;,@progbits
.LLSDA1219:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE1219-.LLSDACSB1219
.LLSDACSB1219:
	.uleb128 .LEHB0-.LFB1219
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB1219
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L11-.LFB1219
	.uleb128 0
	.uleb128 .LEHB2-.LFB1219
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE1219:
	.text
	.size	_Z3foov, .-_Z3foov
	.globl	_Z4foo2v
	.type	_Z4foo2v, @function
_Z4foo2v:
.LFB1220:
	.cfi_startproc
	.cfi_personality 0x3,__gxx_personality_v0
	.cfi_lsda 0x3,.LLSDA1220
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	leaq	29(%rsp), %rdx
	movl	$.LC0, %esi
	leaq	16(%rsp), %rdi
.LEHB3:
	call	_ZNSsC1EPKcRKSaIcE
.LEHE3:
	movq	16(%rsp), %rax
	movq	-24(%rax), %rdx
	movl	$1000000000, %esi
	movl	$0, %ebx
	jmp	.L14
.L18:
	addq	%rdx, %rbx
	addl	$1, %eax
	movslq	%eax, %rcx
	cmpq	%rdx, %rcx
	jb	.L18
.L17:
	subl	$1, %esi
	je	.L16
.L14:
	testq	%rdx, %rdx
	je	.L17
	movl	$0, %eax
	jmp	.L18
.L16:
	movl	$5, %edx
	movl	$.LC1, %esi
	movl	$_ZSt4cout, %edi
.LEHB4:
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	%rbx, %rsi
	movl	$_ZSt4cout, %edi
	call	_ZNSo9_M_insertImEERSoT_
	movb	$10, 15(%rsp)
	movl	$1, %edx
	leaq	15(%rsp), %rsi
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.LEHE4:
	movq	16(%rsp), %rax
	leaq	-24(%rax), %rdi
	cmpq	$_ZNSs4_Rep20_S_empty_rep_storageE, %rdi
	je	.L13
	leaq	-8(%rax), %rdx
	movl	$_ZL28__gthrw___pthread_key_createPjPFvPvE, %ecx
	testq	%rcx, %rcx
	je	.L20
	movl	$-1, %eax
	lock xaddl	%eax, (%rdx)
	jmp	.L21
.L20:
	movl	-8(%rax), %edx
	leal	-1(%rdx), %ecx
	movl	%ecx, -8(%rax)
	movl	%edx, %eax
.L21:
	testl	%eax, %eax
	jg	.L13
	leaq	30(%rsp), %rsi
	call	_ZNSs4_Rep10_M_destroyERKSaIcE
	jmp	.L13
.L23:
	movq	%rax, %rbx
	movq	16(%rsp), %rax
	leaq	-24(%rax), %rdi
	leaq	31(%rsp), %rsi
	call	_ZNSs4_Rep10_M_disposeERKSaIcE
	movq	%rbx, %rdi
.LEHB5:
	call	_Unwind_Resume
.LEHE5:
.L13:
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1220:
	.section	.gcc_except_table
.LLSDA1220:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE1220-.LLSDACSB1220
.LLSDACSB1220:
	.uleb128 .LEHB3-.LFB1220
	.uleb128 .LEHE3-.LEHB3
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB4-.LFB1220
	.uleb128 .LEHE4-.LEHB4
	.uleb128 .L23-.LFB1220
	.uleb128 0
	.uleb128 .LEHB5-.LFB1220
	.uleb128 .LEHE5-.LEHB5
	.uleb128 0
	.uleb128 0
.LLSDACSE1220:
	.text
	.size	_Z4foo2v, .-_Z4foo2v
	.globl	main
	.type	main, @function
main:
.LFB1223:
	.cfi_startproc
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	call	clock
	movq	%rax, %rbx
	call	_Z3foov
	call	clock
	subq	%rbx, %rax
	movq	%rax, %rsi
	movl	$_ZSt4cout, %edi
	call	_ZNSo9_M_insertIlEERSoT_
	movb	$10, 14(%rsp)
	movl	$1, %edx
	leaq	14(%rsp), %rsi
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	call	clock
	movq	%rax, %rbx
	call	_Z4foo2v
	call	clock
	subq	%rbx, %rax
	movq	%rax, %rsi
	movl	$_ZSt4cout, %edi
	call	_ZNSo9_M_insertIlEERSoT_
	movb	$10, 15(%rsp)
	movl	$1, %edx
	leaq	15(%rsp), %rsi
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$0, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1223:
	.size	main, .-main
	.type	_GLOBAL__sub_I__Z3foov, @function
_GLOBAL__sub_I__Z3foov:
.LFB1377:
	.cfi_startproc
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	movl	$_ZStL8__ioinit, %edi
	call	_ZNSt8ios_base4InitC1Ev
	movl	$__dso_handle, %edx
	movl	$_ZStL8__ioinit, %esi
	movl	$_ZNSt8ios_base4InitD1Ev, %edi
	call	__cxa_atexit
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE1377:
	.size	_GLOBAL__sub_I__Z3foov, .-_GLOBAL__sub_I__Z3foov
	.section	.init_array,&quot;aw&quot;
	.align 8
	.quad	_GLOBAL__sub_I__Z3foov
	.local	_ZStL8__ioinit
	.comm	_ZStL8__ioinit,1,1
	.weakref	_ZL28__gthrw___pthread_key_createPjPFvPvE,__pthread_key_create
	.hidden	__dso_handle
	.ident	&quot;GCC: (GNU) 4.9.0 20130414 (experimental)&quot;
	.section	.note.GNU-stack,&quot;&quot;,@progbits
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2330899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330899</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 13 Jun 2013 18:55:06 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:03:05 GMT]]></title><description><![CDATA[<p>CodeA oder CodeB? <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/2330900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330900</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:06:12 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>aber das mit den zwei Zeigern versteh ich irgendwie noch nicht</p>
</blockquote>
<p>Da hat dich Sone auch verarscht. Es sind in Wirklichkeit 3 Zeiger (Start, Stringende, Memoryblockende).</p>
<p>Warum 3 Zeiger und nicht 2+Länge? Ja weil der übliche Weg einen String durchzuiterieren Iteratoren sind</p>
<pre><code class="language-cpp">for(string::iterator it=s.begin(), end=send(); it!=end; ++it)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2330901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330901</guid><dc:creator><![CDATA[scherzkeks]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:06:12 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:13:09 GMT]]></title><description><![CDATA[<p>scherzkeks schrieb:</p>
<blockquote>
<p>Opti schrieb:</p>
<blockquote>
<p>aber das mit den zwei Zeigern versteh ich irgendwie noch nicht</p>
</blockquote>
<p>Da hat dich Sone auch verarscht. Es sind in Wirklichkeit 3 Zeiger (Start, Stringende, Memoryblockende).</p>
</blockquote>
<p>Der dritte spielt hier keine Rolle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330904</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:13:09 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:21:52 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>CodeA oder CodeB? <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>Ich hab mal ganz direkt den hier genommen, obwohl ich natürlich einen hundert mal einfacheren nehmen sollte:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;ctime&gt;

void foo()
{
    std::string s = &quot;Hello World!&quot;;
    int length = s.length();

    uint64_t sum = 0;
    for(int i = 0; i &lt; 1000000000; ++i)
    {
        for(int j = 0; j &lt; length; ++j)
        {
            sum += length;
        }
    }

    std::cout &lt;&lt; &quot;sum: &quot; &lt;&lt; sum &lt;&lt; '\n'; /// Die Ausgaben sind da, damit sum nicht wegoptimiert wird (volatile funktioniert nicht - as-if)
}

void foo2()
{
    std::string s = &quot;Hello World!&quot;;

    uint64_t sum = 0;
    for(int i = 0; i &lt; 1000000000; ++i)
    {
        for(int j = 0; j &lt; s.length(); ++j)
        {
            sum += s.length();
        }
    }

    std::cout &lt;&lt; &quot;sum: &quot; &lt;&lt; sum &lt;&lt; '\n';
}

class stop_watch
{
    clock_t first;

public:

    void start()
    {
        first = clock();
    }

    clock_t elapsed() const
    {
        return clock() - first;
    }
};

int main()
{
    stop_watch sw;
    sw.start();

    foo();

    std::cout &lt;&lt; sw.elapsed() &lt;&lt; '\n';
    sw.start();

    foo2();

    std::cout &lt;&lt; sw.elapsed() &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2330906</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330906</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:21:52 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:27:28 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>Ok danke,<br />
aber das mit den zwei Zeigern versteh ich irgendwie noch nicht,<br />
was soll das bringen, bzw. warum sollte das schneller als eine Addition mit __int64 sein?</p>
</blockquote>
<p>Schneller nicht. Aber es muss dann im jeden Schritt, um die Länge zu bestimmen, eine Subtraktion durchgeführt werden - durchaus kritisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330908</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:27:28 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:31:03 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Opti schrieb:</p>
<blockquote>
<p>Ok danke,<br />
aber das mit den zwei Zeigern versteh ich irgendwie noch nicht,<br />
was soll das bringen, bzw. warum sollte das schneller als eine Addition mit __int64 sein?</p>
</blockquote>
<p>Schneller nicht. Aber es muss dann im jeden Schritt, um die Länge zu bestimmen, eine Subtraktion durchgeführt werden - durchaus kritisch.</p>
</blockquote>
<p>Wobei das Ergebnis immer gleich bleibt und der Optimierer die Subtraktion aus der Schleife herausziehen könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330909</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:31:03 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Thu, 13 Jun 2013 19:32:54 GMT]]></title><description><![CDATA[<p><a href="http://gcc.godbolt.org/" rel="nofollow">http://gcc.godbolt.org/</a><br />
Lass ich mal hier.</p>
<pre><code>#include &lt;string&gt;

using namespace std;

void foo()
{
  string s = &quot;Hello World!&quot;; 
  int length = s.length(); 

  unsigned long long sum = 0;

  for(int i = 0; i &lt; 1000000000; ++i) 
  { 
      for(int j = 0; j &lt; length; ++j) 
      { 
          sum += length; 
      } 
  }
}
</code></pre>
<pre><code>.LC0:
	.string	&quot;Hello World!&quot;
foo():
	subq	$40, %rsp
	movl	$.LC0, %esi
	leaq	16(%rsp), %rdi
	leaq	15(%rsp), %rdx
	call	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;)
	movq	16(%rsp), %rax
	leaq	-24(%rax), %rdi
	cmpq	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_Rep::_S_empty_rep_storage, %rdi
	jne	.L9
.L1:
	addq	$40, %rsp
	ret
.L9:
	leaq	16(%rdi), %rcx
	movl	__gthrw_pthread_cancel(unsigned long), %edx
	testq	%rdx, %rdx
	je	.L4
	orl	$-1, %edx
	lock xaddl	%edx, (%rcx)
.L5:
	testl	%edx, %edx
	jg	.L1
	leaq	15(%rsp), %rsi
	call	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_Rep::_M_destroy(std::allocator&lt;char&gt; const&amp;)
	jmp	.L1
.L4:
	movl	-8(%rax), %edx
	leal	-1(%rdx), %ecx
	movl	%ecx, -8(%rax)
	jmp	.L5
</code></pre>
<pre><code>#include &lt;string&gt;

using namespace std;

void foo()
{
  string s = &quot;Hello World!&quot;; 
  int length = s.length(); 

  unsigned long long sum = 0;

  for(int i = 0; i &lt; 1000000000; ++i) 
  { 
      for(int j = 0; j &lt; s.length(); ++j) 
      { 
          sum += s.length(); 
      } 
  }
}
</code></pre>
<pre><code>.LC0:
	.string	&quot;Hello World!&quot;
foo():
	subq	$40, %rsp
	movl	$.LC0, %esi
	leaq	15(%rsp), %rdx
	leaq	16(%rsp), %rdi
	call	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;)
	movq	16(%rsp), %rdx
	leaq	-24(%rdx), %rdi
	cmpq	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_Rep::_S_empty_rep_storage, %rdi
	jne	.L9
.L1:
	addq	$40, %rsp
	ret
.L9:
	leaq	16(%rdi), %rcx
	movl	__gthrw_pthread_cancel(unsigned long), %eax
	testq	%rax, %rax
	je	.L4
	movl	$-1, %eax
	lock xaddl	%eax, (%rcx)
.L5:
	testl	%eax, %eax
	jg	.L1
	leaq	15(%rsp), %rsi
	call	std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::_Rep::_M_destroy(std::allocator&lt;char&gt; const&amp;)
	addq	$40, %rsp
	ret
.L4:
	movl	-8(%rdx), %eax
	leal	-1(%rax), %ecx
	movl	%ecx, -8(%rdx)
	jmp	.L5
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2330911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330911</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Thu, 13 Jun 2013 19:32:54 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 07:55:40 GMT]]></title><description><![CDATA[<p>von mir noch der Hinweis: std::string ist von Compiler zu Compiler sehr unterschiedlich implementiert, demzufolge wird auch CodeA und CodeB unterschiedlich optimiert</p>
<p>der GCC std::string enthält nur einen Pointer, alle anderen Daten sind in dem Speicherbereich, auf den der Pointer vereist. der MSVC std::string enthält einen 16 Byte Buffer für kleine Strings</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2330993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2330993</guid><dc:creator><![CDATA[dd++ 0]]></dc:creator><pubDate>Fri, 14 Jun 2013 07:55:40 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 19:53:46 GMT]]></title><description><![CDATA[<p>Aber wie kann es sein, dass string::length() schneller ist als eine einfache Addition?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331240</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331240</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Fri, 14 Jun 2013 19:53:46 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:26:02 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>Aber wie kann es sein, dass string::length() schneller ist als eine einfache Addition?</p>
</blockquote>
<p>-O3 nennt man auch den Götterhammer.<br />
Der macht alles, was Du trivialerweise auch per Hand könntest. Und noch viele Tricks, die Du noch gar nicht kennst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331246</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:26:02 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:35:15 GMT]]></title><description><![CDATA[<p>Opti schrieb:</p>
<blockquote>
<p>Wieso ist CodeA langsamer als CodeB?</p>
<p>CodeA:</p>
<pre><code>string s = &quot;Hello World!&quot;;
int length = s.length();
	
unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; length; ++j)
	{
		sum += length;
	}
}
</code></pre>
<p>CodeB:</p>
<pre><code>string s = &quot;Hello World!&quot;;

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; s.length(); ++j)
	{
		sum += s.length();
	}
}
</code></pre>
<p>CodeA braucht 11540ms.<br />
CodeB braucht 7090ms.</p>
</blockquote>
<p>Ein guter Compiler müsste das auf 1ms reduzieren, ist ja alles konstant.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331249</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331249</guid><dc:creator><![CDATA[also]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:35:15 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:47:19 GMT]]></title><description><![CDATA[<p>also schrieb:</p>
<blockquote>
<p>Ein guter Compiler müsste das auf 1ms reduzieren, ist ja alles konstant.</p>
</blockquote>
<p>Meinst du, der Compiler erkennt, dass sum = s.length()^2 * 10^9 ist und die Schleifen weg lässt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331250</guid><dc:creator><![CDATA[Opti]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:47:19 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:51:29 GMT]]></title><description><![CDATA[<p>also schrieb:</p>
<blockquote>
<p>Opti schrieb:</p>
<blockquote>
<p>Wieso ist CodeA langsamer als CodeB?</p>
<p>CodeA:</p>
<pre><code>string s = &quot;Hello World!&quot;;
int length = s.length();
	
unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; length; ++j)
	{
		sum += length;
	}
}
</code></pre>
<p>CodeB:</p>
<pre><code>string s = &quot;Hello World!&quot;;

unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
{
	for(int j = 0; j &lt; s.length(); ++j)
	{
		sum += s.length();
	}
}
</code></pre>
<p>CodeA braucht 11540ms.<br />
CodeB braucht 7090ms.</p>
</blockquote>
<p>Ein guter Compiler müsste das auf 1ms reduzieren, ist ja alles konstant.</p>
</blockquote>
<p>Genau. Habe den Code gerade nicht probiert. Aber habe die GCC mit -O3 schon oft erlebt, wie sie verschachtelte Summierschleifen erkannt hat und zu einer simplen Multiplikation aufgelöst hat. Einfach frech. Superfrech.<br />
Kommt wohl auch ein wenig daher, daß jeder Hans und Kunz dran rumpopeln darf (uns soll!). Wenn ein H&amp;K am eigenen Code sieht, daß da eine Optimierung vom Compiler übernommen werden könnte, fummelt er sie halt rein.<br />
Derzeit macht GCC schnelleren Code als MS. So viele Ideen kann man nicht einkaufen.</p>
<p>Aber MSVC6.1 war härter drauf. Ein Messprogramm, das mit new[] Speicher besorgt hat und nur den Speicher in komplextester Weise beschrieben, gelesen, verrechnet hat, und danach ihn mit delete[] sauber gelöscht hat, aber keine Ausgabe hatte, wurde zu 0ms optimiert. Soweit ich mich erinnere, sind aktuelle Compiler nicht mehr so frech.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331252</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:52:29 GMT]]></title><description><![CDATA[<p>Aber ein guter Compiler kann das:</p>
<pre><code class="language-cpp">// Ursprungscode (platzsparend formatiert)
string s = &quot;Hello World!&quot;;
unsigned __int64 sum = 0;
for(int i = 0; i &lt; 1000000000; ++i)
    for(int j = 0; j &lt; s.length(); ++j)
        sum += s.length();

// loop interchange
for(int j = 0; j &lt; s.length(); ++j)
    for(int i = 0; i &lt; 1000000000; ++i)
        sum += s.length();

// Die 1000000000 Additionen in eine Multiplikation
for(int j = 0; j &lt; s.length(); ++j)
    sum += s.length()*1000000000;

// Die s.length() Additionen in eine Multiplikation
unsigned __int64 sum = 0;
sum += s.length()*s.length()*1000000000;

// =0 ist unnötig
string s = &quot;Hello World!&quot;;
unsigned __int64 sum = s.length()*s.length()*1000000000;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331253</guid><dc:creator><![CDATA[interchanger]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:52:29 GMT</pubDate></item><item><title><![CDATA[Reply to Warum wird der Code nicht optimiert? on Fri, 14 Jun 2013 20:54:41 GMT]]></title><description><![CDATA[<p>Letzter Schritt:</p>
<pre><code class="language-cpp">// unsigned __int64 sum wird nicht gebraucht; einfach weglassen
string s = &quot;Hello World!&quot;;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331255</guid><dc:creator><![CDATA[interchanger]]></dc:creator><pubDate>Fri, 14 Jun 2013 20:54:41 GMT</pubDate></item></channel></rss>