<?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[Very slow operator float ???]]></title><description><![CDATA[<p>Hello.</p>
<p>I created the color class as follows:</p>
<pre><code class="language-cpp">struct Component
{ //One component of color
	float value; //The value of component

	inline operator float(void) const {return value;} //In order to operate with components as with float numbers

	... //Some other methods and useful conversion operators.
};

struct Color
{ //Color
	Component r,g,b;

	... //Some other methods and useful conversion operators.
};
</code></pre>
<p>Now I doing (for example) the following:</p>
<pre><code class="language-cpp">Color c,c1,c2;
...
c.r.value = c1.r + c2.r*2.0; //In fact I overloaded operator= also, but I omit it here for simplicity
c.g.value = c1.g + c2.g*2.0;
c.b.value = c1.b + c2.b*2.0;
</code></pre>
<p>In fact my formulas much more complex, but idea the same. The program running time was 40 seconds. I accidentally changed one formula in the program to something like <code>c.r.value = c1.r.value + c2.r.value*2.0;</code> and noticed small speed-up. Then I commented out the <code>Component::operator float</code> and fixed all errors by adding <code>.value</code> everywhere:</p>
<pre><code class="language-cpp">Color c,c1,c2;
...
c.r.value = c1.r.value + c2.r.value*2.0; //Notice .value here
c.g.value = c1.g.value + c2.g.value*2.0;
c.b.value = c1.b.value + c2.b.value*2.0;
</code></pre>
<p>The program running time reduced to 26 seconds!!!</p>
<p>How could this happen? I sought that <code>inline operator float</code> will work just as if I write <code>.value</code> . All optmizations was turned on! Visual Studio 2008.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223567/very-slow-operator-float</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 19:18:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223567.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 27 Sep 2008 16:17:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Very slow operator float ??? on Sat, 27 Sep 2008 16:17:50 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>I created the color class as follows:</p>
<pre><code class="language-cpp">struct Component
{ //One component of color
	float value; //The value of component

	inline operator float(void) const {return value;} //In order to operate with components as with float numbers

	... //Some other methods and useful conversion operators.
};

struct Color
{ //Color
	Component r,g,b;

	... //Some other methods and useful conversion operators.
};
</code></pre>
<p>Now I doing (for example) the following:</p>
<pre><code class="language-cpp">Color c,c1,c2;
...
c.r.value = c1.r + c2.r*2.0; //In fact I overloaded operator= also, but I omit it here for simplicity
c.g.value = c1.g + c2.g*2.0;
c.b.value = c1.b + c2.b*2.0;
</code></pre>
<p>In fact my formulas much more complex, but idea the same. The program running time was 40 seconds. I accidentally changed one formula in the program to something like <code>c.r.value = c1.r.value + c2.r.value*2.0;</code> and noticed small speed-up. Then I commented out the <code>Component::operator float</code> and fixed all errors by adding <code>.value</code> everywhere:</p>
<pre><code class="language-cpp">Color c,c1,c2;
...
c.r.value = c1.r.value + c2.r.value*2.0; //Notice .value here
c.g.value = c1.g.value + c2.g.value*2.0;
c.b.value = c1.b.value + c2.b.value*2.0;
</code></pre>
<p>The program running time reduced to 26 seconds!!!</p>
<p>How could this happen? I sought that <code>inline operator float</code> will work just as if I write <code>.value</code> . All optmizations was turned on! Visual Studio 2008.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589141</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589141</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 27 Sep 2008 16:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sat, 27 Sep 2008 16:28:50 GMT]]></title><description><![CDATA[<p>You might have disabled inlining (Debug-Build?!).</p>
<p>/edit: Aye, obviously not. Still, that's the only thing I can imagine that could have gone wrong here.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589146</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Sat, 27 Sep 2008 16:28:50 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sat, 27 Sep 2008 17:15:55 GMT]]></title><description><![CDATA[<p>You are right!</p>
<p>The project was imported from WS2005 to WS2008, and projects settings were messed up!</p>
<p>I checked settings in VS2005 and forgot to recheck them in VS2008 <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589174</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589174</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 27 Sep 2008 17:15:55 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sun, 28 Sep 2008 06:25:46 GMT]]></title><description><![CDATA[<p>I still have the following problem:</p>
<pre><code class="language-cpp">float a;
double b;

a += b; //Fast, but warning here about &quot;possible loss of data&quot;
a += static_cast&lt;float&gt;( b ); //No warning, but VERY SLOW
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1589421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589421</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sun, 28 Sep 2008 06:25:46 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sun, 28 Sep 2008 08:07:45 GMT]]></title><description><![CDATA[<p>ich bezweifle, dass es da einen laufzeitunterschied gobt, da static_casts dem compiler eigendlich nur mitteilt, dass die konvertierung gewollt ist.<br />
Es sollte eigendlich identischer code generiert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589451</guid><dc:creator><![CDATA[vlad_tepesch]]></dc:creator><pubDate>Sun, 28 Sep 2008 08:07:45 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sun, 28 Sep 2008 10:07:11 GMT]]></title><description><![CDATA[<p>Well, I get following assembler-output (Visual C++ 2008, full optimizations).</p>
<pre><code class="language-asm">; 6    : 	a += b;

fld	DWORD PTR ?a@@3MA			; a
fadd	QWORD PTR ?b@@3NA			; b
fstp	DWORD PTR ?a@@3MA			; a
</code></pre>
<pre><code class="language-asm">; 11   : 	a += static_cast &lt;float&gt; (b);

fld	QWORD PTR ?b@@3NA			; b
fstp	DWORD PTR tv133[esp+4]
fld	DWORD PTR tv133[esp+4]
fadd	DWORD PTR ?a@@3MA			; a
fstp	DWORD PTR ?a@@3MA			; a
</code></pre>
<p>Somehow it seems to be correct, what SAn described. You can get the same assembler-output for both operations, if you enable &quot;Floating-Point-Model: Fast&quot; in section &quot;Code Generation&quot;. Precise and Strict will not work either.</p>
<p>Gruß (Sorry for my English, but I think/hope you understood it)<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589512</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589512</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sun, 28 Sep 2008 10:07:11 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sun, 28 Sep 2008 10:52:57 GMT]]></title><description><![CDATA[<p>Don06 schrieb:</p>
<blockquote>
<p>You can get the same assembler-output for both operations, if you enable &quot;Floating-Point-Model: Fast&quot; in section &quot;Code Generation&quot;.</p>
</blockquote>
<p>Thank you! This really helps me.</p>
<p>By the way, how do you managed to produce assembler output in Visual Studio?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589548</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589548</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sun, 28 Sep 2008 10:52:57 GMT</pubDate></item><item><title><![CDATA[Reply to Very slow operator float ??? on Sun, 28 Sep 2008 13:27:21 GMT]]></title><description><![CDATA[<p>C/C++ -&gt; Output Files -&gt; Assembler Output<br />
You should have diffrent options, I prefer &quot;Assembly With Source Code&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1589659</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1589659</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sun, 28 Sep 2008 13:27:21 GMT</pubDate></item></channel></rss>