<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Convert degree to Radians effective way</title>
		<link>http://www.allegro.cc/forums/view/617477</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 30 Jun 2018 02:25:08 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was rotating a bitmap around a point and when it completed a rotation it had a flickering effect. So i logged the values of radians and degree. <br />Log at the end of the rotation.<br />[Info   ]: 358   rad= 5.96667<br />[Info   ]: 359   rad= 5.98333<br />[Info   ]: 0   rad= 0<br />[Info   ]: 1   rad= 0.0166667</p><p>But when I converted the 5.98333 back to the degree in google I got 342.8195564<br />This is the function i am using. <br />const float PI = 104348 / 33215;<br />float RenderingEngine::_toRadians(const float&amp; rotDegree)<br />{<br />	return((rotDegree * PI) / 180.0f);<br />}</p><p>Please help the project is also on Git.<br /><a href="https://github.com/ThinkalVB/NearEarth">https://github.com/ThinkalVB/NearEarth</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thinkal VB)</author>
		<pubDate>Tue, 26 Jun 2018 15:32:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>104348 is an integer, 33215 is an integer, so / is the integer division and results in integer 3. Not good enough <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>Try to use the standard library&#39;s M_PI if it&#39;s defined, and if you REALLY have to devise your own, use an explicit literal definition with a number of digits that matches the float type. Both methods are better than 104348.0 / 33215.0 (which is what you wanted to do)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 26 Jun 2018 15:52:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;cmath&gt;/// for M_PI</span>

<span class="p">#define RAD_TO_DEG(rad) (rad*180.0/M_PI)</span>
<span class="p">#define DEG_TO_RAD(deg) (deg*M_PI/180.0)</span>
</pre></div></div><p>
That&#39;s all you need.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 26 Jun 2018 16:56:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Audric&#39;s got the right idea. Use the standard library (or <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_PI"><span class="a">ALLEGRO_PI</span></a></span>), but regardless, it&#39;s good practice to write any expression involving real numbers (not just integers) with <span class="source-code">.</span>-suffixed literals:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> one_third <span class="k3">=</span> <span class="n">1</span>. <span class="k3">/</span> <span class="n">3</span>.<span class="k2">;</span>
</pre></div></div><p>

<b>EDIT</b> replaced <span class="source-code">AL_PI</span> above, forgot the latter is A4. What it is to be an old-timer...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Tue, 26 Jun 2018 17:08:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I did say before to use floating point literals in your other thread, and showed you how as well.....
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 26 Jun 2018 17:27:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I always calculate the 180/M_PI or M_PI/180 ahead of time and store the result in a floating point variable (unless you&#39;re doing NASA simulations, you don&#39;t really need double), though I imagine the compiler will optimize that, I like to be sure, seeing as how that part never changes.  Otherwise I do it how Edgar does.  It&#39;s pretty simple.</p><p>I&#39;m not sure why anyone is trying to calculate PI when it is already available, that&#39;s like re-inventing the wheel.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Tue, 26 Jun 2018 20:13:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use <a href="https://github.com/liballeg/allegro5/blob/f2b8e95d195bb6fb263c8027448964ab3b205248/include/allegro5/base.h#L93">ALLEGRO_PI</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Tue, 26 Jun 2018 20:13:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>And Peter Hull wins!  <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Tue, 26 Jun 2018 20:14:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you Audric, Edgar Reynaldo, dthompson, Neil Roy, Peter Hull<br />Thank you all - I knew I was missing something; I was so dump stupid enough to miss that Ahhhhhh..........<br />I thought my compiler is going nuts <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /><img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /><img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> <br />I used ALLEGRO_PI , Better now
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thinkal VB)</author>
		<pubDate>Tue, 26 Jun 2018 22:58:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038164#target">Neil Roy</a> said:</div><div class="quote"><p>I always calculate the 180/M_PI or M_PI/180 ahead of time and store the result in a floating point variable (unless you&#39;re doing NASA simulations, you don&#39;t really need double), though I imagine the compiler will optimize that</p></div></div><p>
Depends on how it&#39;s written. If you do <span class="source-code">val <span class="k3">*</span> <span class="n">180</span>.<span class="n">0f</span> <span class="k3">/</span> M_PI</span>, the compiler can&#39;t optimize because math isn&#39;t associative in C. You&#39;ll get (slightly) different results depending on whether the multiplication or division is done first, so you need to explicitly order it: <span class="source-code">val <span class="k3">*</span> <span class="k2">(</span><span class="n">180</span>.<span class="n">0f</span> <span class="k3">/</span> M_PI<span class="k2">)</span></span>, then the compiler can pre-calculate the division result. It&#39;s the same as using integers: <span class="source-code">val <span class="k3">/</span> <span class="n">100</span> <span class="k3">*</span> <span class="n">100</span></span> can&#39;t be transformed to <span class="source-code">val <span class="k3">*</span> <span class="n">100</span> <span class="k3">/</span> <span class="n">100</span></span> and thus optimized to <span class="source-code">val <span class="k3">*</span> <span class="n">1</span></span> (or just <span class="source-code">val</span>) because the order alters the result.</p><p>Additionally, <span class="source-code">M_PI</span> is a double, so it&#39;ll promote any math using it to double unless explicitly casted or until assigned to a variable.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Wed, 27 Jun 2018 01:17:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Interesting. I had a look on godbolt, see <a href="https://godbolt.org/g/WFtbpR">https://godbolt.org/g/WFtbpR</a><br />Just to add to what Kitty said, if you have <span class="source-code">ang <span class="k3">*</span> M_PI <span class="k3">/</span> <span class="n">180</span></span> it is compiled to two floating point operations (mul then div) <i>unless</i> you bracket them i.e. <span class="source-code">ang <span class="k3">*</span> <span class="k2">(</span>M_PI <span class="k3">/</span> <span class="n">180</span><span class="k2">)</span></span> or use <span class="source-code"><span class="k3">-</span>ffast-math</span></p><p>So I suppose Neil&#39;s pre-computed constants are <b>100% faster</b> than the naive approach <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /> Well done, sir.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Wed, 27 Jun 2018 02:35:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038177#target">Kitty Cat</a> said:</div><div class="quote"><p>Additionally, M_PI is a double</p></div></div><p>
I didn&#39;t know that, I looked it up, and it seems that all decimal literals are indeed considered double. So, just performing + 0.0 or * 1.0 is enough to promote the computation to double.<br />The only exceptions are when using format modifiers (#define F_PI 3.1415<b>f</b>) or compilation instructions (GCC&#39;s -fsingle-precision-constant). Or, I guess, on CPUs that don&#39;t support double arithmetics.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038178#target">Peter Hull</a> said:</div><div class="quote"><p>So I suppose Neil&#39;s pre-computed constants are 100% faster than the naive approach</p></div></div><p>
Since the 90s, counting instruction no longer tells how much time it will take.<br />The instruction pipeline can reduce this +100% to +10%, and the difference can even be zero if speculative execution can perform the whole computation with the unused &quot;spots&quot; of the pipeline, while it&#39;s waiting for a &quot;if&quot; condition to be resolved.<br />Math plays very well with the pipeline, as it has no side effects.<br />It&#39;s more important to avoid expensive conversions (integer &lt;-&gt; floating point) than to try reduce the number of individual mathematics operators.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Wed, 27 Jun 2018 15:04:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038185#target">Audric</a> said:</div><div class="quote"><p> Since the 90s, counting instruction no longer tells how much time it will take.
</p></div></div><p>Well, I was only kidding, but... can pipelining help if the input of the second op-code is the result of the first? Besides which, for some of us, 1990s is still &#39;recently invented&#39; <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Wed, 27 Jun 2018 15:59:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I assumed so, but it annoyed me that your jest could be taken at face value - a lot of coders are never taught about what modern computers actually do.
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038186#target">Peter Hull</a> said:</div><div class="quote"><p>can pipelining help if the input of the second op-code is the result of the first?</p></div></div><p>
I am really not a specialist, but I think so. When you have a computation A + B + C, you don&#39;t have to wait for the first addition to be completed before you fetch the value of C from memory.<br />Everything I&#39;ve read about optimizing indicates that the people who design CPUs (and compilers) are smarter than us.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Wed, 27 Jun 2018 16:52:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038187#target">Audric</a> said:</div><div class="quote"><p>Everything I&#39;ve read about optimizing indicates that the people who design CPUs (and compilers) are smarter than us.</p></div></div><p>

When I started out programming, I can remember thinking that in order to thoroughly optimise my code, I&#39;d have to pre-compute all of my constants or they&#39;d be continually re-evaluated at runtime. So I&#39;d write stuff like:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define SCREEN_WIDTH  320</span>
<span class="p">#define SCREEN_HEIGHT 200</span>
<span class="p">#define PLAYER_WIDTH  15</span>
<span class="p">#define PLAYER_HEIGHT 20</span>

<span class="p">#define PLAYER_MAX_X  304</span>
<span class="p">#define PLAYER_MAX_Y  179</span>
</pre></div></div><p>

...rather than defining the last two in a more readable way, that even a 90s compiler wouldn&#39;t hesitate to evaluate at compile-time:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define PLAYER_MAX_X  (SCREEN_WIDTH  - PLAYER_WIDTH  - 1)</span>
<span class="p">#define PLAYER_MAX_Y  (SCREEN_HEIGHT - PLAYER_HEIGHT - 1)</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Wed, 27 Jun 2018 18:48:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;cmath&gt;/// for M_PI</span>

<span class="k1">const</span> <span class="k1">double</span> RAD_TO_DEG <span class="k3">=</span> <span class="n">180</span>.<span class="n">0</span><span class="k3">/</span>M_PI<span class="k2">;</span>
<span class="k1">const</span> <span class="k1">double</span> DEG_TO_RAD <span class="k3">=</span> M_PI<span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span>

<span class="k1">double</span> circle_radians <span class="k3">=</span> <span class="n">2</span>.<span class="n">0</span><span class="k3">*</span>M_PI<span class="k2">;</span>
<span class="k1">double</span> circle_degrees <span class="k3">=</span> circle_radians<span class="k3">*</span>RAD_TO_DEG<span class="k2">;</span>
</pre></div></div><p>
That&#39;s better.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038188#target">dthompson</a> said:</div><div class="quote"><p>
When I started out programming, I can remember thinking that in order to thoroughly optimise my code, I&#39;d have to pre-compute all of my constants or they&#39;d be continually re-evaluated at runtime. So I&#39;d write stuff like:
</p></div></div><p>
It depends how you declare them. The compiler can&#39;t always optimize things away unless you mark them as const, but it still does a good job at doing so.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 27 Jun 2018 21:50:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038185#target">Audric</a> said:</div><div class="quote"><p>and the difference can even be zero if speculative execution can perform the whole computation</p></div></div><p>

<a href="https://www.tomshardware.com/news/intel-processors-lazyfp-speculative-execution,37302.html">Intel CPUs Affected By Yet Another Speculative Execution Flaw</a></p><p>I wouldn&#39;t count on that working. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>In one of my projects (my City3D) I use...</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#define RAD2DEG 57.295780f          // Multiply by this to convert radians to degrees</span>
</pre></div></div><p>

...I just straight up done the math and inserted the result of 180/PI in there.  No guessing as to whether it will do the math with that! <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Wed, 27 Jun 2018 23:22:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="https://jsperf.com/diamond-angle-vs-atan2/2">https://jsperf.com/diamond-angle-vs-atan2/2</a></p><p>Generally faster. More numerically stable.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (relpatseht)</author>
		<pubDate>Thu, 28 Jun 2018 08:59:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038189#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>It depends how you declare them.</p></div></div><p>

<span class="source-code"><span class="k1">const</span></span> wasn&#39;t a thing back when I started C. Is it now generally preferred over <span class="source-code"><span class="p">#define</span></span>d constants?</p><p><b>EDIT</b> StackOverflow, amirite? Most of the answers I&#39;ve seen say that they&#39;re pretty useful (though even moreso in C++ than C). Even <span class="source-code"><span class="k1">enum</span></span>s are suggested in <a href="https://stackoverflow.com/a/1674459/1993313">this answer</a>, yet <a href="https://stackoverflow.com/a/1674118/1993313">this one</a> makes an interesting case against <span class="source-code"><span class="k1">const</span></span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Thu, 28 Jun 2018 13:12:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think const fields are probably going to be favored in C++, where there are classes and namespaces so you have the means to &quot;store&quot; a constant in the most logical (and thus user-friendly) context. :<br /><span class="source-code"><span class="k1">if</span> <span class="k2">(</span>sound:Volume <span class="k3">&lt;</span> sound:MaxVolume<span class="k2">)</span> sound:IncreaseVolume<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span></p><p>In C you won&#39;t have these facilities, so constant values will be in the global namespace. I rarely see numeric consts in C codebase, when I do it&#39;s generally because the constant is actually stored in the module, and thus it can change by just recompiling this module and re-linking with the others :
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// items.h</span>
<span class="k1">extern</span> T_ITEM items<span class="k2">[</span><span class="k2">]</span><span class="k2">;</span> <span class="c">// This array has items_count elements</span>
<span class="k1">extern</span> <span class="k1">const</span> <span class="k1">int</span> items_count<span class="k2">;</span>

<span class="c">// items.c</span>
T_ITEM items<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>
<span class="k2">{</span><span class="s">"sword"</span>, NULL<span class="k2">}</span>,
<span class="k2">{</span><span class="s">"axe"</span>, NULL<span class="k2">}</span>,
<span class="k2">}</span><span class="k2">;</span>
<span class="c">// automatic count</span>
<span class="k1">const</span> <span class="k1">int</span> items_count <span class="k3">=</span> <span class="k1">sizeof</span><span class="k2">(</span>items<span class="k2">)</span> <span class="k3">/</span> <span class="k1">sizeof</span><span class="k2">(</span>items<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
You can add and remove items in the array, items_count gets updated automatically, and you don&#39;t have to recompile every file that #include &quot;items.h&quot;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 28 Jun 2018 14:44:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve found that the (implicit) scoping with <span class="source-code"><span class="p">#define</span></span>s by compilation unit - ie. putting &#39;private&#39; constants in .c files, while &#39;public&#39; constants go in .h files - works fine in most cases. Though it&#39;s obviously not as readable as C++&#39;s proper namespacing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Thu, 28 Jun 2018 15:25:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>C++ has <span class="source-code">constexptr</span>, which forces a variable to be treated as a constant expression. So if you have <span class="source-code">constexpr <span class="k1">float</span> rad2deg <span class="k3">=</span> <span class="n">180</span>.<span class="n">0</span><span class="k3">/</span>M_PI<span class="k2">;</span></span>, then <span class="source-code">rad2deg</span> is a constant expression equal to <span class="source-code"><span class="k2">(</span><span class="k1">float</span><span class="k2">)</span><span class="k2">(</span><span class="n">180</span>.<span class="n">0</span><span class="k3">/</span>M_PI<span class="k2">)</span></span>.</p><p>C unfortunately doesn&#39;t have this. Generally, a <span class="source-code"><span class="k1">static</span> <span class="k1">const</span></span> will work okay, since if you have optimizations enabled the compiler will know it can&#39;t be modified, be able to see the value at compile time, then substitute use of the variable with the value directly, except in cases where the language requires a true constant expression. If you need a true constant expression, or want to share it between sources in a header, a macro will typically be better.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 29 Jun 2018 03:09:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>D also constant&#39;ifies literally anything it can, and allows (nested!) function calls to be included as long as they can be computed at compile-time. Newest C++ finally adds what D had for a decade.</p><p>But this is an incredible amount of discussion on something so meaningless. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> One extra multiply will not affect anything unless it&#39;s actually tens of thousands of them a second in a super tight loop like a particle system with tens of thousands of elements (or preferably, way more). The second you make a virtual function call or allocated a piece of memory, you&#39;ve blown any affect a multiply would have.</p><p>Moreover, CPU&#39;s regularly shuffle the order of code around, and cache branch results to the point that you can&#39;t even guarantee any particular line of code will improve without actual empirical testing. </p><p>So yeah, it&#39;s a good thing to know that specifying constants help stupid compilers realize they&#39;re stupid (or is the flaw more in the lack of expressiveness in the language? /digression), and it&#39;s a good &quot;trick&quot; to have in your &quot;bag of tricks&quot; when you&#39;re doing an optimizing path. The actual practical usefulness of this is pretty low.</p><p>At the end of the day, use whatever code that makes you the MOST EFFICIENT at producing working, maintainable code that gets out the door. Because 100% of the optimized code that never ships... doesn&#39;t matter.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Fri, 29 Jun 2018 04:25:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>While I agree in general with the conclusion of Chris&#39; post, I have to say that if you repeat yourself then you&#39;re neglecting the DRY principle. There&#39;s no justifiable reason to repeat yourself. I see it constantly in real world code by colleagues and it makes me sick. Business software is often OO just because, and you will routinely see blocks of code that are dereferencing the same nested properties/objects over and over again. Maybe a sophisticated compiler will see that and optimize it, but honestly it shouldn&#39;t have to. It&#39;s far more readable if the human stores a pointer or reference along the way for a nested object/struct that is accessed more than once. And instead of repeatedly instructing the computer to follow pointer after pointer you can just go straight to the pointer that you&#39;re interested in for a negligible stack allocation. This kind of copy/pasta repeated code drives me insane.</p><p>Ultimately, algorithms are king. And where performance is key you need to be smarter than I about data structures. Worrying about it at the CPU level is kind of missing the point unless you know what CPUs are going to be used. But you can try to optimize for the systems you are targeting if it matters. Otherwise, DRY, offload what you can to the compiler, and save yourself the effort when you can afford to. A skilled &quot;engineer&#39;s&quot; time is worth way more than machine time. Depending on the problem it&#39;s probably more economical to just throw more hardware at it versus hiring a wizard to make the most of the hardware you have. And if you are legit part of team that is worth spending the human time to make it fast then you probably won&#39;t need any advice from here.</p><p>I applaud Neil for defining a constant instead of repeating himself. And I facepalm for everybody that didn&#39;t think of it because it&#39;s the simplest thing you can possibly achieve in a program so what the Hell else are you doing wrong if you don&#39;t?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 29 Jun 2018 08:40:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038222#target">bamccaig</a> said:</div><div class="quote"><p> It&#39;s far more readable if the human stores a pointer or reference along the way for a nested object/struct that is accessed more than once.</p></div></div><p>
I do agree in cases where there it makes the code shorter
</p><div class="source-code snippet"><div class="inner"><pre>format<span class="k2">(</span>currentSite.Domain.DomainGroup.Category.Order, 
       currentSite.Domain.DomainGroup.Category.Name, 
       currentSite.Domain.DomainGroup.Category.Type<span class="k2">)</span><span class="k2">;</span>
<span class="c">//</span>
category <span class="k3">=</span> currentSite.Domain.DomainGroup.Category<span class="k2">;</span>
format<span class="k2">(</span>category.Order, category.Name, category.Type<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
VB.NET even has a nice language construct for this
</p><div class="source-code snippet"><div class="inner"><pre>Using currentSite.Domain.DomainGroup.Category<span class="k2">;</span>
   format<span class="k2">(</span>.Order, .Name, .Type<span class="k2">)</span><span class="k2">;</span>
End Using
</pre></div></div><p>

But as a strict rule ?
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// I really don't think the following:</span>
format<span class="k2">(</span>currentSite.Domain.Type, currentSite.Domain.Name, nextSite.Domain.Type, nextSite.Domain.Name<span class="k2">)</span><span class="k2">;</span>
<span class="c">// Should be rewritten as</span>
Domain domainOfTheCurrentSite <span class="k3">=</span> currentSite.Domain<span class="k2">;</span>
Domain domainOfTheNextSite <span class="k3">=</span> nextSite.Domain<span class="k2">;</span>
format<span class="k2">(</span>domainOfTheCurrentSite.Type, domainOfTheCurrentSite.Name, domainOfTheNextSite.Type, domainOfTheNextSite.Name<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Fri, 29 Jun 2018 12:54:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038222#target">bamccaig</a> said:</div><div class="quote"><p>It&#39;s far more readable if the human stores a pointer or reference along the way for a nested object/struct that is accessed more than once.</p></div></div><p>

Do compilers generally figure out and optimise repeated dereferencing? ie.:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">for</span><span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> PARTICLES_SIZE<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  particles<span class="k2">[</span>i<span class="k2">]</span>.x <span class="k3">+</span><span class="k3">=</span> particles<span class="k2">[</span>i<span class="k2">]</span>.dx<span class="k2">;</span>
<span class="number">  4</span>  particles<span class="k2">[</span>i<span class="k2">]</span>.y <span class="k3">+</span><span class="k3">=</span> particles<span class="k2">[</span>i<span class="k2">]</span>.dy<span class="k2">;</span>
<span class="number">  5</span>  particles<span class="k2">[</span>i<span class="k2">]</span>.life--<span class="k2">;</span>
<span class="number">  6</span><span class="k2">}</span>
<span class="number">  7</span>
<span class="number">  8</span><span class="c">// would the above be effectively optimised into the below?</span>
<span class="number">  9</span>
<span class="number"> 10</span>PARTICLE<span class="k3">*</span> particle<span class="k2">;</span>
<span class="number"> 11</span><span class="k1">for</span><span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> PARTICLES_SIZE<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="number"> 12</span><span class="k2">{</span>
<span class="number"> 13</span>  particle <span class="k3">=</span> <span class="k3">&amp;</span>particles<span class="k2">[</span>i<span class="k2">]</span><span class="k2">;</span>
<span class="number"> 14</span>
<span class="number"> 15</span>  particle-&gt;x <span class="k3">+</span><span class="k3">=</span> particle-&gt;dx<span class="k2">;</span>
<span class="number"> 16</span>  particle-&gt;y <span class="k3">+</span><span class="k3">=</span> particle-&gt;dy<span class="k2">;</span>
<span class="number"> 17</span>  particle-&gt;life--<span class="k2">;</span>
<span class="number"> 18</span><span class="k2">}</span>
</div></div><p>

I&#39;ve unfortunately been in the habit of repeatedly dereferencing arrays since I began writing C, yet would never do it in any other language. <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Fri, 29 Jun 2018 13:50:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038226#target">dthompson</a> said:</div><div class="quote"><p> Do compilers generally figure out and optimise repeated dereferencing?
</p></div></div><p>It seems that they can, see <a href="https://godbolt.org/g/8fXFPN">here</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Fri, 29 Jun 2018 16:31:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Awesome. That compiler explorer thing looks like a nice tool - interesting to see how compilation output differs between gcc and clang
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dthompson)</author>
		<pubDate>Fri, 29 Jun 2018 18:11:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve used such tools sometimes, but it can be hard to build a program that sets up a situation for what you want to test : if the compiler detects that your program is pointless (ie: you do computations, but the result goes nowhere), it can consider it dead code and scrap it entirely from the output.</p><p>Also, sample 5-lines programs are never a real-life situation, they&#39;re a fairyland where all data is on the same memory page, pipeline and registers are at your full disposal, and you&#39;re free to leave them in any state when you&#39;re done, as you&#39;re the last piece of code in the executable.<br />On the other hand, some optimization can be gotten ONLY when there is code beforehand : If the previous block was guaranteed to leave 0 in register EAX, you can re-use this register for a variable initialized to zero at no cost.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Fri, 29 Jun 2018 19:58:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038224#target">Audric</a> said:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// I really don't think the following:</span>
format<span class="k2">(</span>currentSite.Domain.Type, currentSite.Domain.Name, nextSite.Domain.Type, nextSite.Domain.Name<span class="k2">)</span><span class="k2">;</span>
<span class="c">// Should be rewritten as</span>
Domain domainOfTheCurrentSite <span class="k3">=</span> currentSite.Domain<span class="k2">;</span>
Domain domainOfTheNextSite <span class="k3">=</span> nextSite.Domain<span class="k2">;</span>
format<span class="k2">(</span>domainOfTheCurrentSite.Type, domainOfTheCurrentSite.Name, domainOfTheNextSite.Type, domainOfTheNextSite.Name<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div></div><p>

You seem to be choosing unnecessarily verbose names here. The references are literally right next to their definitions. They can be single letters or abbreviations for all intents and purposes without affecting readability.</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Easier to read IMO, but still results in a somewhat long</span>
<span class="c">// statement that should be broken onto many lines.</span>
Domain currentDomain <span class="k3">=</span> currentSite.Domain<span class="k2">;</span>
Domain nextDomain <span class="k3">=</span> nextSite.Domain<span class="k2">;</span>

format<span class="k2">(</span>currentDomain.Type, currentDomain.Name,
       nextDomain.Type, nextDomain.Name<span class="k2">)</span><span class="k2">;</span>

<span class="c">// Still easier to read IMO, but barely longer than the original.</span>
Domain cd <span class="k3">=</span> currentSite.Domain<span class="k2">;</span>
Domain nd <span class="k3">=</span> nextSite.Domain<span class="k2">;</span>

format<span class="k2">(</span>cd.Type, cd.Name, nd.Type, nd.Name<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

A couple of things that I want to point out: white-space is your friend. Adding a blank line between the variable definitions/assignments and the function call aides greatly in readability. Similarly, your original line is too damn long. I don&#39;t care if my monitor is that long. It&#39;s more efficient to read up and down instead of back and forth, up and down.</p><p>Also, I&#39;m hoping this is Java or something with implicit references because otherwise we&#39;re probably copying the domain objects unnecessarily. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sat, 30 Jun 2018 00:42:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617477/1038226#target">dthompson</a> said:</div><div class="quote"><p>Do compilers generally figure out and optimise repeated dereferencing?</p></div></div><p>
It depends. There are what&#39;s known as aliasing rules. C, C++, and other languages say when it is or isn&#39;t possible for two separate pointers/references to refer to the same memory, and if you&#39;re writing to a pointer/reference than can potentially alias the same memory as you&#39;re reading from, regardless of whether or not they are actually aliasing, the compiler may be forced to generate less optimized code just in case. The compiler has to be able to prove to itself that the value can&#39;t change between multiple dereferences to optimize it. This can be helped by not using <span class="source-code"><span class="k3">-</span>fno-strict-aliasing</span> (which disabled the stricter aliasing rules, in turn making the compiler assume more pointers/references may alias than are otherwise allowed to) and also proper use of the <span class="source-code">restrict</span> keyword, which tells the compiler the given pointer/reference will not alias other memory being used during its lifetime.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 30 Jun 2018 02:25:08 +0000</pubDate>
	</item>
</rss>
