I can’t find any documentation from Apple to explain why this piece of code runs at different speeds depending on how many times its been run.
- (void)speedTest2:(CIImage*)source {
NSTimeInterval start = CFAbsoluteTimeGetCurrent();
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setValue:source forKey:kCIInputImageKey];
CGImageRef cgImage = [_context createCGImage:filter.outputImage fromRect:source.extent];
UIImage* output = [UIImage imageWithCGImage:cgImage];
if (cgImage)
CFRelease(cgImage);
_source.image = output;
NSLog(@"time: %0.3fms", 1000.0f * (CFAbsoluteTimeGetCurrent() - start));
}
Run times
- Fresh app install – first call to method = 206ms
- App restarted – first call to method = 61ms
- second call to method (3rd, 4th, …) = 14ms
The same source image is being used for every run.
I know Core Image concatenates the filter chain. Is this somehow being cached? Can I pre-cache this operation so users don’t get hit with performance problems on their first app launch?
This one is making me crazy 🙁
A portion of the overhead may be the image library itself loading. If the effects are implemented as pixel shaders, there may well be a compilation step going on behind the scenes.
This hidden cost is unavoidable, but you can choose to do it at a more convenient time. For example when the application is loading.
I would suggest loading a small image (1×1 px) and applying some effects to it during load to see if it helps.
You may also want to try the official Apple forums for a response.