【问题标题】:Similar Lines of Code, Vastly Different Performance相似的代码行,截然不同的性能
【发布时间】:2012-08-20 19:25:18
【问题描述】:

我正在为 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% 对 3.5%)?

注意:[DrawingState sharedState].screenWidth 和 [DrawingState sharedState].screenHeight 分别是 768 和 1024,所以理论上,我正在做相同数量的绘图。

【问题讨论】:

    标签: objective-c ios performance core-graphics


    【解决方案1】:

    由于您使用的是抽样,因此这可能无法完全准确地反映您的时间花在哪里。

    我会试试这个代码:

    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 :)

    【讨论】:

    • 正是我要建议的!虽然sharedState 方法调用的实际 时间可能很少,但它对该行的共享​​> 时间的影响可能很大。
    • 让我试一试并回复。但是,为了记录,[DrawingState sharedState].context 和 context 是不一样的!它们是两种不同的上下文。
    • 等等,我刚刚用最后一条评论回答了我的问题!在“上下文”中,我只绘制到屏幕的一部分(基于 drawRect 传递的矩形)。在 [DrawingState sharedState].context 中,我每次都绘制到屏幕全尺寸的上下文!感谢您建议我将它们分开,这样更容易看出它们是不同的上下文并引用不同的大小。
    • 那会有所作为!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2018-10-03
    • 2010-12-16
    • 2013-01-06
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多