Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benchmark UIView drawRect: method

I am writing an iPhone application in Objective-C that utilizes some custom drawing in views and I would like to benchmark various revisions of my code to see what really helps. I was planing on doing this by setting up a new application, adding my custom drawing code to the drawRect: method of the view, then, in a for loop in the view controller, sending [UIView setNeedsDisplay] some large number of times and timing how long it takes to complete. However the setNeedsDisplay calls seem to be cached so even though I call it 1000 times in a for loop, the drawRect: method gets called only once. Also, I tried calling drawRect: directly but I need a graphics context to do some drawing in and when I don't use setNeedsDisplay: UIGraphicsGetCurrentContext() doesn't give me a context.

Any suggestions?

Thanks,

Kyle

like image 938
Kyle Avatar asked May 12 '09 00:05

Kyle


1 Answers

You can benchmark a view by doing the following:

- (NSTimeInterval)timeTakenToDrawView:(UIView *)view count:(NSInteger)count
{
    CGRect bounds = [view bounds];
    NSDate *startDate = [NSDate date];
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSInteger i = 0;
    for (i = 0; i < count; i++) {
        CGContextSaveGState(context);
        [view drawRect:bounds];
        CGContextRestoreGState(context);
    }
    UIGraphicsEndImageContext();
    return [[NSDate date] timeIntervalSinceDate:startDate];
}

(Haven't tried it, but it should work)

like image 121
rpetrich Avatar answered Sep 28 '22 00:09

rpetrich