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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With