<?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[Return by reference sehr langsam??]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich probiere einen vector aus einer Funktion per reference zurückzugeben. Ich habe dabei bemerkt, dass das aber verdammt langsam ist. Gleich langsam, als wenn ich eine Kopie des vectors zurückgebe.<br />
Ich habe wirklich keine Ahnung warum das so ist. Kann mir das bitte wer erklären?</p>
<pre><code>#include &lt;vector&gt;
using namespace std;

struct st {
	float a, b, c;
};

class A {
public:
	vector&lt;st&gt; vec;
	void initialize() {
		for(int i = 0; i &lt; 900; i++)
			vec.push_back(st());
	}
	vector&lt;st&gt;* getValues1() {
		return &amp;vec;
	}

	vector&lt;st&gt;&amp; getValues2() {
		return vec;
	}

	vector&lt;st&gt; getValues3() {
		return vec;
	}
};

void main() {
	A a;
	a.initialize();
	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt;* t = a.getValues1(); // fast
	}

	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt; t = a.getValues2(); // slow
	}

	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt; t = a.getValues3(); // slow
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/306216/return-by-reference-sehr-langsam</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 06:28:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306216.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 21 Jul 2012 13:12:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:12:54 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich probiere einen vector aus einer Funktion per reference zurückzugeben. Ich habe dabei bemerkt, dass das aber verdammt langsam ist. Gleich langsam, als wenn ich eine Kopie des vectors zurückgebe.<br />
Ich habe wirklich keine Ahnung warum das so ist. Kann mir das bitte wer erklären?</p>
<pre><code>#include &lt;vector&gt;
using namespace std;

struct st {
	float a, b, c;
};

class A {
public:
	vector&lt;st&gt; vec;
	void initialize() {
		for(int i = 0; i &lt; 900; i++)
			vec.push_back(st());
	}
	vector&lt;st&gt;* getValues1() {
		return &amp;vec;
	}

	vector&lt;st&gt;&amp; getValues2() {
		return vec;
	}

	vector&lt;st&gt; getValues3() {
		return vec;
	}
};

void main() {
	A a;
	a.initialize();
	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt;* t = a.getValues1(); // fast
	}

	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt; t = a.getValues2(); // slow
	}

	for(int i = 0; i &lt; 50000; i++) {
		vector&lt;st&gt; t = a.getValues3(); // slow
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2234546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234546</guid><dc:creator><![CDATA[nightrider]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:12:54 GMT</pubDate></item><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:15:03 GMT]]></title><description><![CDATA[<p>Was erwartest du, wenn du a) ohne optimierungen compilierst und b) explizit den Rückgabewert kopierst?</p>
<p>oh, und mach das initialize weg und benutze stattdessen ordentliche Konstruktoren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234547</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234547</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:15:03 GMT</pubDate></item><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:22:04 GMT]]></title><description><![CDATA[<p>Danke für deine schneller Antwort. Ist nur ein Beispielprogramm, muss nicht schön sein. <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="🙂"
    /><br />
Also die Funktion getValues2() sollte doch eine Referenz zurückgeben. Also gebe ich doch nicht den kompletten vector zurück. Sollte das nicht gleich schnell sein, als wenn ich per pointer (getValues1) zurückgebe?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234548</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234548</guid><dc:creator><![CDATA[nightrider]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:22:04 GMT</pubDate></item><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:25:13 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">vector&lt;st&gt; t = a.getValues2();
</code></pre>
<p>Die Funktion selbst eine Referenz zurück, aber trotzdem wird zu t kopiert. Ändere das zu</p>
<pre><code class="language-cpp">vector&lt;st&gt;&amp; t = a.getValues2();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2234549</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234549</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:25:13 GMT</pubDate></item><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:27:09 GMT]]></title><description><![CDATA[<p>otze schrieb:</p>
<blockquote>
<p>Was erwartest du, wenn du a) ohne optimierungen compilierst <strong>und b) explizit den Rückgabewert kopierst?</strong></p>
<p>oh, und mach das initialize weg und benutze stattdessen ordentliche Konstruktoren.</p>
</blockquote>
<p>Du kopierst explizit den Rückgabewert, nightrider.</p>
<pre><code class="language-cpp">vector&lt;st&gt; t = a.getValues2();
</code></pre>
<p>t wird hier zu einer Kopie.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234550</guid><dc:creator><![CDATA[KMT]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:27:09 GMT</pubDate></item><item><title><![CDATA[Reply to Return by reference sehr langsam?? on Sat, 21 Jul 2012 13:36:13 GMT]]></title><description><![CDATA[<p>Stimmt, so geht das, danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234552</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234552</guid><dc:creator><![CDATA[nightrider]]></dc:creator><pubDate>Sat, 21 Jul 2012 13:36:13 GMT</pubDate></item></channel></rss>