Search code examples
iosvdsp

Speed up summing components in a vector


I would like to create an array from another array by summing the components in blocks of four, e.g.:

float inVector[256];
float outVector[64];

for(int i=0; i<64; i++){
  for(int j=0; j<4; j++){
    int k = 4*i + j;
    outVector[i] += inVector[k];
  }
}

I would like to accelerate this. I have looked in the available libraries in iOS like vDSP and vForce, but haven't found anything that fits. The closest candidate has been vDSP_vswsum, but that doesn't do what I want. Does anyone have a tip about how to speed this up?


Solution

  • You're trying to decimate a vector. vDSP_sve with N=4 will speed up your inner loop. if you eventually want the average of the 4 values, VDSP_mean.