iOS Accelerate Framework vImage - Performance improvement?

前端 未结 3 1113
忘了有多久
忘了有多久 2021-02-10 16:05

I\'ve been working with OpenCV and Apple\'s Accelerate framework and find the performance of Accelerate to be slow and Apple\'s documentation limited. Let\'s take for example:

3条回答
  •  生来不讨喜
    2021-02-10 16:37

    Don't keep re-allocating vImage_Buffer if you can avoid it.

    One thing that is critical to vImage accelerate performance is the reuse of vImage_Buffers. I can't say how many times I read in Apple's limited documentation hints to this effect, but I was definitely not listening.

    In the aforementioned blur code example, I reworked the test app to setup the vImage_Buffer input and output buffers once per image rather than once for each call to boxBlur. I dropped <10ms per call which made a noticeable difference in response time.

    This says that Accelerate needs time to warm-up before you start seeing performance improvements. The first call to this method took 34ms.

    - (UIImage *)boxBlurWithSize:(int)boxSize
    {
        vImage_Error error;
        error = vImageBoxConvolve_ARGB8888(&_inputImageBuffer,
                                           &_outputImageBuffer,
                                           NULL,
                                           0,
                                           0,
                                           boxSize,
                                           boxSize,
                                           NULL,
                                           kvImageEdgeExtend);
        if (error) {
            NSLog(@"vImage error %zd", error);
        }
    
        CGImageRef modifiedImageRef = vImageCreateCGImageFromBuffer(&_outputImageBuffer,
                                                                    &_inputImageFormat,
                                                                    NULL,
                                                                    NULL,
                                                                    kvImageNoFlags,
                                                                    &error);
    
        UIImage *returnImage = [UIImage imageWithCGImage:modifiedImageRef];
        CGImageRelease(modifiedImageRef);
    
        return returnImage;
    }
    

提交回复
热议问题