<?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[Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..?]]></title><description><![CDATA[<p>Hallo,</p>
<p>in mehreren Funktionen müssen in bestimmten Fällen zwei long-Variablen getauscht werden (Min &gt; Max) und ich frage mich, welche Technik die schnellste ist:</p>
<p>Ein Swap per Referenz</p>
<pre><code>void _Swap(long &amp;a, long &amp;b)
{
  long c = a;
  a = b;
  b = c;
}
</code></pre>
<p>oder ein Selbstaufruf mit vertauschten Variablen:<br />
(Beispielfunktion)</p>
<pre><code>bool PointInEllipse(long px, long py, long left, long top, long right, long bottom)
{
  if (left &gt; right) // normalisiertes Container-Rechteck erzwingen
  {
    _Swap(left, right);
    // oder
    return PointInEllipse(px, py, right, top, left, bottom);
  }
  // ..
}
</code></pre>
<p>Oder gibt es noch weitere, schnellere Alternativen (abgesehen davon, _Swap() praktisch in der Funktion selbst durchzuführen)?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/133248/performance-variablentausch-min-gt-max-selbstaufruf-by-reference</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 21:44:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/133248.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Jan 2006 13:32:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 13:32:48 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>in mehreren Funktionen müssen in bestimmten Fällen zwei long-Variablen getauscht werden (Min &gt; Max) und ich frage mich, welche Technik die schnellste ist:</p>
<p>Ein Swap per Referenz</p>
<pre><code>void _Swap(long &amp;a, long &amp;b)
{
  long c = a;
  a = b;
  b = c;
}
</code></pre>
<p>oder ein Selbstaufruf mit vertauschten Variablen:<br />
(Beispielfunktion)</p>
<pre><code>bool PointInEllipse(long px, long py, long left, long top, long right, long bottom)
{
  if (left &gt; right) // normalisiertes Container-Rechteck erzwingen
  {
    _Swap(left, right);
    // oder
    return PointInEllipse(px, py, right, top, left, bottom);
  }
  // ..
}
</code></pre>
<p>Oder gibt es noch weitere, schnellere Alternativen (abgesehen davon, _Swap() praktisch in der Funktion selbst durchzuführen)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968007</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Mon, 16 Jan 2006 13:32:48 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:11:35 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">inline void Swap(long &amp;a, long &amp;b)
{
  a ^= b;
  b ^= a;
  a ^= b;
}
</code></pre>
<p><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/968055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968055</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:11:35 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:11:42 GMT]]></title><description><![CDATA[<p>Ich vermute mal, swap() (als inline-Funktion deklariert) dürfte schneller sein - aber im Ernstfall müsstest du das praktisch ausprobieren.</p>
<p>Zwei Punkte, die für swap() sprechen, wären noch:</p>
<ul>
<li>Du mußt die &lt; Überprüfung nur einmal machen</li>
<li>lokale Variablen der Funktion müssen nur einmal angelegt (und demzufolge auch nur einmal zerstört) werden</li>
</ul>
]]></description><link>https://www.c-plusplus.net/forum/post/968056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968056</guid><dc:creator><![CDATA[CStoll (off)]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:11:42 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:33:40 GMT]]></title><description><![CDATA[<p>Danke euch beiden, werds wohl mit der flotten XOR-Swap-Funktion von LordJaxom lösen!</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>, hast du irgendwas mit Farbrausch zu tun, oder haste den Codeblock auch nur kopiert? <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/968096</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968096</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:33:40 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:36:05 GMT]]></title><description><![CDATA[<p>Cpt_Future schrieb:</p>
<blockquote>
<p>Danke euch beiden, werds wohl mit der flotten XOR-Swap-Funktion von LordJaxom lösen!</p>
</blockquote>
<p>Woher nimmst du das wissen, dass diese Variante schneller ist?<br />
Ist auf einem x86 nämlich nicht der Fall.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968100</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968100</guid><dc:creator><![CDATA[Any]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:36:05 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:43:09 GMT]]></title><description><![CDATA[<p>Nimm einfach std::swap. Der Hersteller deiner Platform wird schon wissen, was am schnellsten ist. Die Funktion wird ja in fast allen Fällen geinlined und dann zahlt sich das Wissen des Herstellers aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968113</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968113</guid><dc:creator><![CDATA[Ponto]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:43:09 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 14:57:43 GMT]]></title><description><![CDATA[<p>Any schrieb:</p>
<blockquote>
<p>Cpt_Future schrieb:</p>
<blockquote>
<p>Danke euch beiden, werds wohl mit der flotten XOR-Swap-Funktion von LordJaxom lösen!</p>
</blockquote>
<p>Woher nimmst du das wissen, dass diese Variante schneller ist?<br />
Ist auf einem x86 nämlich nicht der Fall.</p>
</blockquote>
<p>Ich habe das Wissen nicht, ich habe nur angenommen, dass (wenige) bitweise Operationen schneller sind als das deklarieren neuer Variablen oder neue Funktionsaufrufe.</p>
<p>Also werd ich wohl std::swap() nehmen, danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968121</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968121</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Mon, 16 Jan 2006 14:57:43 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 15:08:04 GMT]]></title><description><![CDATA[<p>Any schrieb:</p>
<blockquote>
<p>Ist auf einem x86 nämlich nicht der Fall.</p>
</blockquote>
<p>Hält mein Kollege für ein Gerücht, was empfiehlst du denn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968136</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Mon, 16 Jan 2006 15:08:04 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 15:41:30 GMT]]></title><description><![CDATA[<p>Ich würde dem Kollegen nur glauben wenn er &quot;Profiler&quot; heißt. <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/968167</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968167</guid><dc:creator><![CDATA[............]]></dc:creator><pubDate>Mon, 16 Jan 2006 15:41:30 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 15:45:38 GMT]]></title><description><![CDATA[<p>Also auf meinem Linux-x86 ist die lokale-Variable-Variante rund 30% schneller als die XOR-Variante... Ohne Optimierung...</p>
<p>Mit -O2 sind beide annähernd gleich schnell <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/968175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968175</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Mon, 16 Jan 2006 15:45:38 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Mon, 16 Jan 2006 16:31:17 GMT]]></title><description><![CDATA[<p>CStoll (off) schrieb:</p>
<blockquote>
<p>Ich vermute mal, swap() (als inline-Funktion deklariert) dürfte schneller sein - aber im Ernstfall müsstest du das praktisch ausprobieren.</p>
<p>Zwei Punkte, die für swap() sprechen, wären noch:</p>
<ul>
<li>Du mußt die &lt; Überprüfung nur einmal machen</li>
<li>lokale Variablen der Funktion müssen nur einmal angelegt (und demzufolge auch nur einmal zerstört) werden</li>
</ul>
</blockquote>
<p>ich denke, es hängt davon ab inwieweit der compiler damit zurecht kommt. man könnte z.b. den test unmittelbar in eine inline function auslagern, mit ein bisschen statischer analyse, könnte der optimierer sie möglicherweise z.T. eliminieren:</p>
<pre><code class="language-cpp">bool PointInEllipse(long px, long py, long left, long top, long right, long bottom)
{
    return left &gt; right
        ? PointInEllipseImpl(px, py, left, top, right, bottom)
        : PointInEllipseImpl(px, py, right, top, left, bottom);
}
bool PointInEllipseImpl(long px, long py, long left, long top, long right, long bottom)
{
// ...
}
</code></pre>
<p>ob das hilft, kann nur der profiler verraten. in jedem fall zieht das argument mit den lokalen variablen nicht wirklich. wenn die swap variante benutzt wird, sollt es auf jeden fall std::swap sein - das ist schließlich dazu da, die optimale implementation auf der gegebenen plattform zu haben.</p>
<pre><code class="language-cpp">inline void Swap(long &amp;a, long &amp;b)
{
  a ^= b;
  b ^= a;
  a ^= b;
}
</code></pre>
<p>ist schlecht. es führt zu erheblichen abhängigkeiten in den registern und dürfte die meisten optimierer verwirren. im übrigen ist es nicht aliasing sicher, eine ganz unangenehme eigenschaft für ein swap (überleg, was bei swap(a,a) rauskommt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968213</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968213</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 16 Jan 2006 16:31:17 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Tue, 17 Jan 2006 08:19:29 GMT]]></title><description><![CDATA[<p>kurzum ich bleib besser bei<br />
long c = a;<br />
a = b;<br />
b = c;</p>
<p>Denn std::swap() ist offenbar nicht auf allen Plattformen die wir einsetzen vorhanden und wenn ^= wie du sagst nicht so optimierbar ist, lass ichs einfach.<br />
So relevant ist die Funktion(sgeschwindigkeit) jetzt auch nicht.<br />
Danke euch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968620</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Tue, 17 Jan 2006 08:19:29 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Tue, 17 Jan 2006 08:35:32 GMT]]></title><description><![CDATA[<p>Ich sehe immer noch keinen Grund std::swap nicht einzusetzen. Notfalls programmiert man es sich nach.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968624</guid><dc:creator><![CDATA[Walli]]></dc:creator><pubDate>Tue, 17 Jan 2006 08:35:32 GMT</pubDate></item><item><title><![CDATA[Reply to Performance Variablentausch (Min &amp;gt; Max): Selbstaufruf, By-Reference, ..? on Tue, 17 Jan 2006 08:44:47 GMT]]></title><description><![CDATA[<p>Ich bin Praktikant, soll eigentlich nur Unit-Tests machen, und mein Betreuer meint &quot;std::swap() sollten wir nicht nehmen, das steht auf einigen Plattformen nicht zur Verfügung.&quot;<br />
Da hat er nunmal das letzte Wort. Es einfach mal so nachzuprogrammieren für bestimmte Plattformen, da fehlt mir das Wissen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/968629</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/968629</guid><dc:creator><![CDATA[Cpt_Future]]></dc:creator><pubDate>Tue, 17 Jan 2006 08:44:47 GMT</pubDate></item></channel></rss>