title: C++ multiple for loop bottleneck
body: <p>my C++ program has encountered a bottleneck. There is a function to cut the rgb32 image. Look at the code:</p>
  
  <pre><code>BOOL Convert2MultiImage(BYTE *p32Img, BYTE** p24Img, int dxSize32, int  dySize32, int row, int vertical)
  {
  
  BYTE *pTemp;
  BYTE** ptr = new BYTE*[row * vertical];
  
  pTemp = p32Img;
  
  for (int num = 0; num &lt; row * vertical; num++)
  {
      ptr[num] = p24Img[num];
  }
  
  int displayWidth = dxSize32 * vertical;
  int width4 = displayWidth * 4;
  int width4_y = displayWidth * dySize32 * 4;
  int x_4 = dxSize32 * 4;
  
  int p = 0;
  int seq = 0;
  
  int  i, j;
  
  for (int r = 0; r &lt; row; r++)
  {
      for (int v = 0; v &lt; vertical; v++)
      {
          for (j = 0; j &lt; dySize32; j++)
          {
              for (i = 0; i &lt; dxSize32; i++)
              {
                  p = (displayWidth * j + i + r * displayWidth * dySize32 + v * dxSize32) * 4;
  
                  *(ptr[seq]++) = pTemp[p];
                  *(ptr[seq]++) = pTemp[p + 1];
                  *(ptr[seq]++) = pTemp[p + 2];
  
              }
          }
          seq++; 
      }
  }
  
  delete[] ptr;
  ptr = NULL;
  
  return true;
  }
  </code></pre>
  
  <p>The parameter dxSize32 = 1920, dySize32 = 1080, row=4, vertical=4; the function means to cut from 8K image into 16 1080P images.</p>
  
  <p>this code takes about 300ms, and I need about 30ms, my The computer CPU is I5-6400 dual core. Can I use some hardware to process this code to reduce the time spent?</p>
  
  <p>NOW I used opencv, very good, now it takes about 30ms.</p>
  
  <pre><code>void COpencvCutImg::BGR32_Cut_MultiBGR24(BYTE* source, BYTE** target, int   width, int height, int row, int vertical)
  {
  Mat matSource = Mat(width, height, CV_8UC4, source);
  
  int height1 = matSource.rows;
  int width1 = matSource.cols;
  
  int ceil_height = height1 / row;
  int ceil_width = width1 / vertical;
  
  Mat roi_img;
  
  int seq = 0;
  for (int i = 0; i&lt;row; i++)
  for (int j = 0; j&lt;vertical; j++){
  
      Rect rect(j*ceil_width, i*ceil_height, ceil_width, ceil_height);
  
      roi_img = matSource(rect);
  
      Mat continuousBGRA(roi_img.size(), CV_8UC3, target[seq]);
      cv::cvtColor(roi_img, continuousBGRA, CV_BGRA2BGR, 3);
  
      seq++;
  }
  </code></pre>
  
  <p>}</p>