<?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[Hi, how can I optimize the following code?]]></title><description><![CDATA[<p>I'm trying to use multiple thread to optimize my following code, but met some problems, anyone can help me?</p>
<p>r[3][500] are initialized.</p>
<pre><code class="language-cpp">int computePot() {
   int i, j;

   for( i=0; i&lt;500; i++ ) {
      for( j=0; j&lt;i-1; j++ ) {
        distx = pow( (r[0][j] - r[0][i]), 2 );
        disty = pow( (r[1][j] - r[1][i]), 2 );
        distz = pow( (r[2][j] - r[2][i]), 2 );
        dist = sqrt( distx + disty + distz );
        pot += 1.0 / dist;
      }
   }
</code></pre>
<p>I doubt that If this code really can be optimized?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/167481/hi-how-can-i-optimize-the-following-code</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 23:38:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/167481.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Dec 2006 16:15:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 16:34:10 GMT]]></title><description><![CDATA[<p>I'm trying to use multiple thread to optimize my following code, but met some problems, anyone can help me?</p>
<p>r[3][500] are initialized.</p>
<pre><code class="language-cpp">int computePot() {
   int i, j;

   for( i=0; i&lt;500; i++ ) {
      for( j=0; j&lt;i-1; j++ ) {
        distx = pow( (r[0][j] - r[0][i]), 2 );
        disty = pow( (r[1][j] - r[1][i]), 2 );
        distz = pow( (r[2][j] - r[2][i]), 2 );
        dist = sqrt( distx + disty + distz );
        pot += 1.0 / dist;
      }
   }
</code></pre>
<p>I doubt that If this code really can be optimized?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190216</guid><dc:creator><![CDATA[qinghunt]]></dc:creator><pubDate>Mon, 11 Dec 2006 16:34:10 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 16:54:09 GMT]]></title><description><![CDATA[<p>You can change this</p>
<pre><code class="language-cpp">distx = pow( (r[0][j] - r[0][i]), 2 );
</code></pre>
<p>into</p>
<pre><code class="language-cpp">distx = (r[0][j] - r[0][i]) * (r[0][j] - r[0][i]);
</code></pre>
<p>pow is slower than a normal multiplication.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190239</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190239</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 11 Dec 2006 16:54:09 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 17:10:07 GMT]]></title><description><![CDATA[<p>you could replace your postfix increment operators in the &quot;for loops&quot; by prefix increment operators (i++ ==&gt; ++i)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190250</guid><dc:creator><![CDATA[Checker*amp*Murckser]]></dc:creator><pubDate>Mon, 11 Dec 2006 17:10:07 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 17:40:16 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int computePot() {
   int i, j;

   for( i=0; i&lt;500; i++ ) {
      int k = i-1;
      for( j=0; j&lt;k; j++ ) {
       [...]
      }
   }
</code></pre>
<p>[/quote]</p>
<p>Its not much, but change the second for-loop condition like so. Reason: The calculation (i-1) probably will be executed on every iteration of the loop. Than again this are roughly 125k iterations, so every single bit counts <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/1190264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190264</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 11 Dec 2006 17:40:16 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 18:04:34 GMT]]></title><description><![CDATA[<p>Use a function for computing the <strong>inverse</strong> square root, for example:</p>
<pre><code class="language-cpp">float InvSqrt( float f )
{
	float y, r;
	int i;

	y = f * .5f;
	i = *reinterpret_cast&lt; int *&gt;( &amp;f );
	i = 0x5f3759df - ( i &gt;&gt; 1 );
	r = *reinterpret_cast&lt; float *&gt;( &amp;i );

	r = r * ( 1.5f - r * r * y );

	return r;
}
</code></pre>
<p>Now the division can be removed from your code:</p>
<pre><code class="language-cpp">pot += InvSqrt( distx + disty + distz );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1190274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190274</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Mon, 11 Dec 2006 18:04:34 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Mon, 11 Dec 2006 19:31:47 GMT]]></title><description><![CDATA[<pre><code>int computePot() {
   int k;
   for(k=0;k&lt;p;k++){
      _beginthread(compute_quarter_pot,0,(void *)(k)); 
   } 
   while(true)
   {
	   if(complete == p)
		   break;
	   continue;
   }
   for(k=0;k&lt;p;k++){ 
      pot+=pots[k];
   } 
   complete = 0;

   return 0;
}

void compute_quarter_pot(void* quarter){
	int m = (int)quarter;
   for(int i = m;i&lt;NPARTS/2;i+=p){ 
       pots[m]+=compute_single_pot(i)+compute_single_pot(NPARTS-1-i); 
   } 
   complete++; 
   _endthread();
}

double compute_single_pot(int i){ 
   double pot=0,x, y, z; 
   int j;
   int k = i -1;
   for( j=0; j&lt;k; j++ ) { 
      x = POW2(r[0][j] - r[0][i]); 
      y = POW2(r[1][j] - r[1][i]); 
      z = POW2(r[2][j] - r[2][i]); 
      pot += 1.0 / sqrt( x + y + z ); 
   } 
   return(pot); 
}
</code></pre>
<p>this is my way to accelerate the code, it works when config as debug, but will be block(without any response and mistake) when config as release. I'm confused. anyway, thanks you guys who reply me. and sorry for my poor english, I'm come from China.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190339</guid><dc:creator><![CDATA[qinghunt]]></dc:creator><pubDate>Mon, 11 Dec 2006 19:31:47 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Tue, 12 Dec 2006 05:37:10 GMT]]></title><description><![CDATA[<p>Ah. You have to use some kind of synchronization here.<br />
Option 1: use events/condition variables<br />
Option 2: use a barrier (if available)<br />
Option 3: use interlocked instructions (but only if you have to)</p>
<p>A condition variable should be exactly what you need. If you're working with Win32 you can easily use an EVENT in this situation.</p>
<p>A barrier is not 100% what you need, but it comes close, and it should be very easy to use. It blocks all threads that &quot;hit&quot; the barrier (i.e. call the &quot;hit&quot; function or however it's called) until exactly N threads have hit the barrier (where N is configurable e.g. upon creation). When the N-th thread hits the barrier, all N threads will continue (i.e. the N-th thread will just &quot;run through&quot; the barrier, releasing all others).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190351</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 12 Dec 2006 05:37:10 GMT</pubDate></item><item><title><![CDATA[Reply to Hi, how can I optimize the following code? on Tue, 12 Dec 2006 09:05:15 GMT]]></title><description><![CDATA[<p>Checker&amp;Murckser schrieb:</p>
<blockquote>
<p>you could replace your postfix increment operators in the &quot;for loops&quot; by prefix increment operators (i++ ==&gt; ++i)</p>
</blockquote>
<p>Sry, but that's a common misbelief. There's no difference in the perfomance of pre- or postfix operators with POD types.<br />
It only gives you a benefit when using iterators.<br />
But it's good practice to always use prefix operators as there's no sideeffect in this case.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1190425</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1190425</guid><dc:creator><![CDATA[__HIRSCH_H__]]></dc:creator><pubDate>Tue, 12 Dec 2006 09:05:15 GMT</pubDate></item></channel></rss>