我正在用Objective-C编写一个iPhone应用程序,它在视图中使用了一些自定义的绘图,我想对我的代码的各种修订进行基准测试,看看哪些是真正有帮助的。我打算这样做:设置一个新的应用程序,将我的自定义绘图代码添加到视图的drawRect:方法中,然后,在视图控制器的for循环中,多次发送[UIView setNeedsDisplay]并计算完成所需的时间。但是,setNeedsDisplay调用似乎被缓存了,所以即使我在for循环中调用了1000次,drawRect:方法也只被调用了一次。另外,我试着直接调用drawRect:,但是我需要一个图形上下文来做一些绘图,当我不使用setNeedsDisplay:时,UIGraphicsGetCurrentContext()不会给我一个上下文。
有什么建议吗?
谢谢,
凯尔
发布于 2009-05-12 02:23:15
您可以通过执行以下操作对视图进行基准测试:
- (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];
}(我还没试过,但应该可以用)
发布于 2009-05-12 01:07:48
在你的NSView的.m里面。显然,您可能希望将其连接到UI或其他什么地方。
- (void)awakeFromNib {
// Have to wait for the window to be onscreen, graphics context ready, etc
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:NULL repeats:NO];
}
- (void)timerFired:(NSTimer *)aTimer {
[self lockFocus];
NSDate* then = [NSDate date];
int callIdx = 0;
for( callIdx = 0; callIdx < 1000; callIdx++ ) {
[self drawRect:[self bounds]];
}
NSDate* now = [NSDate date];
[self unlockFocus];
NSLog(@"Took %f seconds", [now timeIntervalSinceDate:then]);
}
- (void)drawRect:(NSRect)rect {
[[NSColor redColor] set];
NSRectFill(rect);
}发布于 2009-05-12 03:20:21
不要忘记,如果您的drawRect经常使用,您还可以使用CoreImage性能工具并获得帧率。
您还可以使用一些性能工具来查看在该方法中花费了多长时间。
https://stackoverflow.com/questions/850677
复制相似问题