我正在为 iPad 编写一个绘图应用程序,并且正在做一些时间分析,以便我可以看到我的绘图可能会加速到哪里。
在我的绘图方法中,我有以下 4 行代码:
CGContextDrawImage([DrawingState sharedState].context, CGRectMake(0.0f, 0.0f, [DrawingState sharedState].screenWidth, [DrawingState sharedState].screenHeight), unzoomedBufferImage);
CGImageRef image = CGBitmapContextCreateImage([DrawingState sharedState].context);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, 768, 1024), image);
CGImageRelease(image);
第一行占18.8%的时间,第二、三、四行只占4.5%的时间,CGContextDrawImage行占3.5%。
为什么这两个 CGContextDrawImage 函数的性能差别如此之大(18.8% vs 3.5%)?
注意:[DrawingState sharedState].screenWidth 和 [DrawingState sharedState].screenHeight 分别是 768 和 1024,所以理论上,我正在做相同数量的绘图。
最佳答案
由于您使用的是抽样,因此这可能不是您花费时间的完全准确的图片。
我会试试这个代码:
DrawingState * state = [ DrawingState sharedState ] ;
CGContextRef context = state.context ;
CGRect r = { .size = { state.screenWidth, state.screenHeight } } ;
CGContextDrawImage( context, r, unzoomedBufferImage);
CGImageRef image = CGBitmapContextCreateImage( context );
CGContextDrawImage(context, r, image);
CGImageRelease(image);
只是为了获取更多信息。
即当事情没有意义时,试着改变你对你“知道是真的”的假设
如果这没有显示任何信息,您可以尝试使用我的分析宏来检测您的代码:https://gist.github.com/1905396 :)
关于objective-c - 相似的代码行,截然不同的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043823/