MikeS
MikeS

Reputation: 3921

Similar Lines of Code, Vastly Different Performance

I'm writing a drawing app for the iPad and was doing some time profiling so I could see where my drawing could potentially be sped up at.

In my drawing method, I have the following 4 lines of code:

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

The first line accounts for 18.8% of the time, the second, third, and fourth lines only account for 4.5% of the time, with the CGContextDrawImage line accounting for 3.5%.

Why do these two CGContextDrawImage functions have such vastly different performance (18.8% vs 3.5%)?

Note: [DrawingState sharedState].screenWidth and [DrawingState sharedState].screenHeight are 768 and 1024, respectively, so in theory, I'm doing the same amount of drawing.

Upvotes: 1

Views: 147

Answers (1)

nielsbot
nielsbot

Reputation: 16032

Since you are using sampling, this may not be a completely accurate picture of where you're spending your time..

I'd try this code:

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

Just to get more info.

i.e. When things don't make sense, try changing your assumptions about what you "know to be true"

If this doesn't reveal any info, you could try instrumenting your code with my profiling macros: https://gist.github.com/1905396 :)

Upvotes: 6

Related Questions