Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the absolutely fastest for loop in c?
    text
    copied!<p>Im trying to write optimized code for accesing image pixels and need to make a for loop super fast without going down to assembly level. Further more the indexing is done along the rows to minimize cache misses.</p> <p>This is what I have:</p> <pre><code>for (indr=0;indr&lt;(height-1)*width;indr+=width) { for (indc=0;indc&lt;width;indc++){ I[indr+indc]= dostuff ; } } </code></pre> <p>I cant make it a single loop because the "dostuff" includes accessing elements that arent on the same row.</p> <p>Is there a faster way to do this?</p> <p><strong>EDIT</strong> Okay, because my previous post was slightly unclear im adding here the full code. Its pretty unreadable but the general idea is that Im performing a convolution with a simple box using an integral image. The image is first padded with ws+1 zeros on the left and bottom and ws zeros on the right and top. It is then made into an integral image Ii. The following function takes the integral image and extracts the convolution where the result Ic is the same size as the original image. </p> <pre><code>void convI(float *Ic,float *Ii,int ws, int width, int height) { int W=width+ws*2+1,indR; int H=height+ws*2+1,indC; int w=width, indr; int h=height, indc; int jmpA=W*(ws+1),jmpC=W*ws,jmpB=ws+1,jmpD=ws; for (indR=W*(ws+1),indr=0;indr&lt;width*(height-1);indR+=W,indr+=width) { for (indC=ws+1,indc=0;indc&lt;width;indC++,indc++){ //Performs I[indA]+I[indD]-I[indB]-I[indC]; Ic[indr+indc]= Ii[indR-jmpA+indC-jmpB]+ Ii[indR+jmpC+indC+jmpD]- Ii[indR+jmpC+indC-jmpB]- Ii[indR-jmpA+indC+jmpD]; } } } </code></pre> <p>So thats the "dostuff" part. The loop is sluggish.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload