objective-c - 相似的代码行,截然不同的性能

标签 objective-c ios performance core-graphics

我正在为 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/

相关文章:

iphone - NSURLConnection 和处理响应代码和响应数据

r - 加速 R 代码以基于其他数据帧用字符串填充向量

c# - 查找目录中所有文件的更好方法

objective-c - Objective-C 中的非阻塞等待函数

objective-c - 是否有针对 Google 的 Objective-C 风格的 Uncrustify 配置?

ios - 如何在边缘为白色的正方形内绘制完整的 UIImage

mysql - 将 iPhone 应用程序连接到远程数据库服务器

ios - 无法为 'XCTest' 加载底层模块

ios - 在 Core Data 中使用级联规则删除托管对象的性能注意事项

objective-c - NStableview setDatasource EXC_BAD_ACCESS