<?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[Optimierung einer If-Abfrage]]></title><description><![CDATA[<p>Hallo zusammen, ich hätte eine kleine Frage bzgl. einer If-Abfrage...</p>
<pre><code class="language-cpp">if(!(x &amp; 0x10204000000i64 &amp;&amp;
			x &amp; 0x700000i64 &amp;&amp;
			x &amp; 0x4210i64 &amp;&amp;
			x &amp; 0x204001000i64 &amp;&amp;
			x &amp; 0x2082000000i64 &amp;&amp;
			x &amp; 0x1004200i64 &amp;&amp;
			x &amp; 0x82002000i64 &amp;&amp;
			x &amp; 0x2002080i64 &amp;&amp;
			x &amp; 0x2082i64)) return true;
</code></pre>
<p>Wie man unschwer erkennen kann, werden in der If-Abfrage lauter 64-Bit Konstanten verwendet. Jetzt zu meiner Frage: Wäre der Code möglicherhweise schneller, wenn die Konstanten in Arrays abgelegt werden? Bevor jetzt Sprüche kommen wie &quot;Prober das doch selbst aus&quot; muss ich dazu sagen, dass fast 1000 Zeilen die obige Form haben... Ich würde mir in etwa folgendes vorstellen:</p>
<pre><code class="language-cpp">if(!(x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++])) return true;
</code></pre>
<p>Für Anregungen bin ich dankbar....</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/274740/optimierung-einer-if-abfrage</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 07:05:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/274740.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Oct 2010 19:19:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 19:19:34 GMT]]></title><description><![CDATA[<p>Hallo zusammen, ich hätte eine kleine Frage bzgl. einer If-Abfrage...</p>
<pre><code class="language-cpp">if(!(x &amp; 0x10204000000i64 &amp;&amp;
			x &amp; 0x700000i64 &amp;&amp;
			x &amp; 0x4210i64 &amp;&amp;
			x &amp; 0x204001000i64 &amp;&amp;
			x &amp; 0x2082000000i64 &amp;&amp;
			x &amp; 0x1004200i64 &amp;&amp;
			x &amp; 0x82002000i64 &amp;&amp;
			x &amp; 0x2002080i64 &amp;&amp;
			x &amp; 0x2082i64)) return true;
</code></pre>
<p>Wie man unschwer erkennen kann, werden in der If-Abfrage lauter 64-Bit Konstanten verwendet. Jetzt zu meiner Frage: Wäre der Code möglicherhweise schneller, wenn die Konstanten in Arrays abgelegt werden? Bevor jetzt Sprüche kommen wie &quot;Prober das doch selbst aus&quot; muss ich dazu sagen, dass fast 1000 Zeilen die obige Form haben... Ich würde mir in etwa folgendes vorstellen:</p>
<pre><code class="language-cpp">if(!(x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++])) return true;
</code></pre>
<p>Für Anregungen bin ich dankbar....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960248</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960248</guid><dc:creator><![CDATA[PeterPan11]]></dc:creator><pubDate>Fri, 01 Oct 2010 19:19:34 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 19:36:44 GMT]]></title><description><![CDATA[<p>Theoretisch wäre es langsamer (solange der Compiler das nicht wegoptimiert, wovon ich aber ausgehen würde); praktisch ist es auf modernen Computern eh nicht spürbar.</p>
<p>Lass es einfach so.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960251</guid><dc:creator><![CDATA[theliquidwave]]></dc:creator><pubDate>Fri, 01 Oct 2010 19:36:44 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 02:17:52 GMT]]></title><description><![CDATA[<p>PeterPan11 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(!(x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++] &amp;&amp;
			x &amp; a[i++])) return true;
</code></pre>
</blockquote>
<p>Also ich bin jetzt nicht sicher ob das erlaubt ist. Schätze eher nicht. Bzw. ich bin sogar fast sicher dass es nicht erlaubt ist.<br />
(EDIT: doch, ist erlaubt)</p>
<p>Die einzige Möglichkeit wie es erlaubt sein könnte, wäre, wenn &amp;&amp; als Sequence-Point gilt (für &amp;&amp; und || ist ja im Standard Short-Circuiting vorgeschrieben).<br />
(EDIT: und genau so ist es, &amp;&amp; und || sind sequence points)</p>
<p>Achja, wovon ich rede: man darf zwischen zwei Sequence-Points nicht mehrfach die selbe Variable ändern, wie du es mit i++ machen würdest. Also sowas wie <code>printf(&quot;%d, %d\n&quot;, i++, i++)</code> ist NICHT erlaubt.</p>
<p>Aber egal. Es wäre, selbst wenn erlaubt, höchst schwindeliger Code, und daher solltest du es nicht so machen. OK wären natürlich wenn du &quot;i++&quot; durch Konstanten/Literale ersetzt:</p>
<pre><code class="language-cpp">if(!(x &amp; a[0] &amp;&amp;
			x &amp; a[1] &amp;&amp;
			x &amp; a[2] &amp;&amp;
			x &amp; a[3] &amp;&amp;
			x &amp; a[4] &amp;&amp;
			x &amp; a[5] &amp;&amp;
			x &amp; a[6] &amp;&amp;
			x &amp; a[7] &amp;&amp;
			x &amp; a[8])) return true;
</code></pre>
<p>Oder gleich ne Schleife:</p>
<pre><code class="language-cpp">for (size_t i = 0; i &lt; 9; i++)
    if((x &amp; a[i]) == 0)
        return false;
return true;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1960263</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960263</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 02 Oct 2010 02:17:52 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 20:31:21 GMT]]></title><description><![CDATA[<p>sehe ich auch so, wie chrisber, aber:</p>
<pre><code class="language-cpp">if(!(x &amp; 0x10204000000i64 &amp;&amp;
            x &amp; 0x700000i64 &amp;&amp;
            x &amp; 0x4210i64 &amp;&amp;
            x &amp; 0x204001000i64 &amp;&amp;
            x &amp; 0x2082000000i64 &amp;&amp;
            x &amp; 0x1004200i64 &amp;&amp;
            x &amp; 0x82002000i64 &amp;&amp;
            x &amp; 0x2002080i64 &amp;&amp;
            x &amp; 0x2082i64)) return true;
</code></pre>
<p>würd ich niemals in nem quellcode stehen haben wollen... das würde vrmtl so aussehen:</p>
<pre><code class="language-cpp">bool is_strange(long long value)
{
  const long long is_a = 0x10204000000i64;
  const long long have_a = 0x700000i64;
  /*...*/

  return !(value &amp; is_a &amp;&amp; value &amp; have_a);
}

bool foo(long long value)
{
  if(is_strange(value))
    return true;

/*...*/
}
</code></pre>
<p>ansonsten kannst du dir bei so was auch immer gut den erzeugten asm-code angucken - selbst, wenn man nicht all zu viel davon versteht, wird man doch mit ein wenig googlen erkennen, was die cpu mit dem jeweiligen code nacheinander abarbeiten muss.</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960264</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 01 Oct 2010 20:31:21 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 20:51:29 GMT]]></title><description><![CDATA[<p>am besten wäre es, wenn du sowohl if als auch die logischen und's weglässt:</p>
<pre><code class="language-cpp">return (
           (x &amp; 0x10204000000i64) &amp;
           (x &amp; 0x700000i64) &amp;
           (x &amp; 0x4210i64) &amp;
           (x &amp; 0x204001000i64) &amp;
           (x &amp; 0x2082000000i64) &amp;
           (x &amp; 0x1004200i64) &amp;
           (x &amp; 0x82002000i64) &amp;
           (x &amp; 0x2002080i64) &amp;
           (x &amp; 0x2082i64)
        ) == 0i64;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1960271</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960271</guid><dc:creator><![CDATA[sdfsg]]></dc:creator><pubDate>Fri, 01 Oct 2010 20:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 21:00:51 GMT]]></title><description><![CDATA[<p>Wobei bloss leider ganz was anderes rauskommt als beim ursprünglichen Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960273</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960273</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 01 Oct 2010 21:00:51 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Fri, 01 Oct 2010 21:04:17 GMT]]></title><description><![CDATA[<p>gelöscht, war falsch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960274</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 01 Oct 2010 21:04:17 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 01:22:03 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Also ich bin jetzt nicht sicher ob das erlaubt ist. Schätze eher nicht. Bzw. ich bin sogar fast sicher dass es nicht erlaubt ist.</p>
</blockquote>
<p>Doch, es ist erlaubt.</p>
<p>§5.14/2: Logical AND operator schrieb:</p>
<blockquote>
<p>All side effects of the first expression except for destruction of temporaries (12.2) happen before the second expression is evaluated.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1960311</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960311</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Oct 2010 01:22:03 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 02:15:24 GMT]]></title><description><![CDATA[<p>Hihi, OK, steht sogar auf der Wikipedia Seite hier<br />
<a href="http://en.wikipedia.org/wiki/Sequence_point" rel="nofollow">http://en.wikipedia.org/wiki/Sequence_point</a></p>
<p>Gleich der erste Punkt.</p>
<p>Krass <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/1960317</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960317</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 02 Oct 2010 02:15:24 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 12:28:25 GMT]]></title><description><![CDATA[<p>Aber du hast Recht, schön ist sowas wie hier im Beispiel sicher nicht. <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>
<p>Ich würde auch eine Schleife nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960445</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Oct 2010 12:28:25 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 14:14:00 GMT]]></title><description><![CDATA[<p>Gelöscht, war falsch. Ich habe die &amp;&amp; gedanklich übersehen, die ja wegen der Forward-Auswertung etwas ganz Anderes bewirken, als ich &quot;gelesen&quot; habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960491</guid><dc:creator><![CDATA[Yamakuzure]]></dc:creator><pubDate>Sat, 02 Oct 2010 14:14:00 GMT</pubDate></item><item><title><![CDATA[Reply to Optimierung einer If-Abfrage on Sat, 02 Oct 2010 15:37:05 GMT]]></title><description><![CDATA[<p>Nagut, ich denke, ich werd den Code so lassen wie er ist.<br />
Was etwas an Laufzeitvorteil bringen kann ist die Unterscheidung zwischen 32Bit und 64Bit Literalen (Nicht alle müssen zwangsläufig in 64Biz gestopft werden).<br />
Der Compiler scheint an dieser Stelle nicht selbststaändig zu optimieren.</p>
<p>Trotzdem Danke für die vielen Tipps!!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1960530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1960530</guid><dc:creator><![CDATA[PeterPan11]]></dc:creator><pubDate>Sat, 02 Oct 2010 15:37:05 GMT</pubDate></item></channel></rss>